0% found this document useful (0 votes)
2 views2 pages

codingAssignment1(B58)

The document contains several Python functions for string manipulation and list processing. It includes functions to find the length of the longest substring without repeating characters, reverse a string, replace vowels with '#', remove special characters, and find duplicates in a list. Each function is accompanied by example usage and output.

Uploaded by

regularuse0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views2 pages

codingAssignment1(B58)

The document contains several Python functions for string manipulation and list processing. It includes functions to find the length of the longest substring without repeating characters, reverse a string, replace vowels with '#', remove special characters, and find duplicates in a list. Each function is accompanied by example usage and output.

Uploaded by

regularuse0001
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

class Solution:

def lengthOfLongestSubstring(self, s: str) -> int:


char_index = {}
max_length = 0
start = 0

for i, char in enumerate(s):


if char in char_index and char_index[char] >= start:
start = char_index[char] + 1
char_index[char] = i
max_length = max(max_length, i - start + 1)

return max_length

# 2) Reverse a String
def reverse_string(s):
return s[::-1]

print(reverse_string("Geeks"))

skeeG

# 3) Replace all Vowels by ‘#’


def replace_vowels(s):
vowels = "aeiouAEIOU"
return ''.join('#' if char in vowels else char for char in s)

print(replace_vowels("hello world"))

h#ll# w#rld

# 4) Remove all Special Characters


import re
def remove_special_characters(s):
return re.sub(r'[^a-zA-Z0-9\s]', '', s)

print(remove_special_characters("Artificial %#$@ Intelligence $123"))

Artificial Intelligence 123

# 5) Find Duplicates in a List


def find_duplicates(lst):
seen = set()
duplicates = set()

for num in lst:


if num in seen:
duplicates.add(num)
seen.add(num)

return list(duplicates)
print(find_duplicates([2,5,8,4,5,2,3,8,4,6,10,25]))

[8, 2, 4, 5]

You might also like