-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathstring_manipulations.py
More file actions
140 lines (104 loc) · 4.2 KB
/
Copy pathstring_manipulations.py
File metadata and controls
140 lines (104 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
#\queston 1
'''
Reverse a String: Write a function that takes a string as input and returns the string reversed.
For example, if the input is "hello", the output should be "olleh".
reverse_string_approach_1(any) -> None (display)
reverse_string_approach_2(any) -> None (display)
approach 1 using loop
#reverse string using loop
'''
def reverse_string_approach_1(input_string):
reversed_str=''
for char in input_string:
reversed_str=char + reversed_str
return reversed_str
string='kaskikot'
reversed_str=reverse_string_approach_1(string)
print(reversed_str)
'''
approach 2 not using loop (look up from internet)
'''
#without using loop
def reverse_string_approach_2(input_string):
return input_string[::-1]
string="kaskikot"
reversed_str=reverse_string_approach_2(string)
print(reversed_str)
#questionNo.2
'''
Count Vowels in a String: Write a function that takes a string as input and returns the number of vowels (a, e, i, o, u) in the string.
For example, if the input is "hello", the output should be 2.
using loop
'''
def count_vowels(input_string):
vowels = 'aeiouAEIOU'
count = 0
for char in input_string:
if char in vowels:
count += 1
return count
input = "Shequal project"
vowel_count = count_vowels(input)
print(" Total number of vowels are:", vowel_count)
'''
Check Palindrome: Write a function that checks if a given string is a palindrome.
A palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward.
For example, "radar" is a palindrome.
'''
def is_palindrome(input_string):
# Remove non-alphanumeric characters and convert to lowercase
cleaned_string = ''.join(char.lower() for char in input_string if char.isalnum())
# Compare cleaned string with its reverse
return cleaned_string == cleaned_string[::-1]
string_input = "madam"
print(is_palindrome(string_input))
'''String Anagrams: Write a function that checks if two strings are anagrams of each other.
An anagram is a word or phrase formed by rearranging the letters of a different word or phrase,
typically using all the original letters exactly once.
For example, "listen" and "silent" are anagrams.
'''
def string_anagrams(string1, string2):
# Removing non-alphanumeric characters and convert to lowercase
cleaned_str1 = ''.join(char.lower() for char in string1 if char.isalnum())
cleaned_str2 = ''.join(char.lower() for char in string2 if char.isalnum())
return sorted(cleaned_str1) == sorted(cleaned_str2)
str1 = "listen"
str2 = "silent"
print(string_anagrams(str1, str2))
# By take input from user
def string_anagrams(string1, string2):
cleaned_str1 = ''.join(char.lower() for char in string1 if char.isalnum())
cleaned_str2 = ''.join(char.lower() for char in string2 if char.isalnum())
return sorted(cleaned_str1) == sorted(cleaned_str2)
def check_anagrams():
str1 = input("Enter the first string: ")
str2 = input("Enter the second string: ")
if string_anagrams(str1, str2):
print("The strings are anagrams of each other.")
else:
print("The strings are not anagrams of each other.")
check_anagrams()
'''#Optional Try your best
Longest Substring Without Repeating Characters: Given a string, find the length of the longest substring without repeating characters.
For example, the longest substring without repeating characters in "abcabcbb" is "abc",
which has a length of 3.
'''
def longest_substring_length(input_string):
# Initialize variables
max_length = 0
start = 0
char_index_map = {}
# Iterate through the string
for i, char in enumerate(input_string):
# If the character is already in the map and its index is after the start of the current substring
if char in char_index_map and char_index_map[char] >= start:
start = char_index_map[char] + 1
# Update the index of the current character
char_index_map[char] = i
# Calculate the length of the current substring
current_length = i - start + 1
# Update the maximum length
max_length = max(max_length, current_length)
return max_length
input_str = "abcabcbb"
print(longest_substring_length(input_str))