1)
input_string = "guvi geeks network private limited"
vowels = "aeiou"
vowel_counts = {vowel: 0 for vowel in vowels}
total_vowels = 0
for char in input_string:
char_lower = char.lower()
if char_lower in vowels:
# Increment the count for this vowel
vowel_counts[char_lower] += 1
# Increment the total vowel count
total_vowels += 1
Print the results
print("Total number of vowels:", total_vowels)
print("Count of each individual vowel:")
for vowel, count in vowel_counts.items():
print(f"{vowel.upper()}: {count}")
2) # Initialize the number to start from
current_number = 1
Iterate through each level of the pyramid
for i in range(1, 21):
# Print numbers for the current level
for j in range(i):
if current_number > 20:
break
print(current_number, end=" ")
current_number += 1
# Move to the next line after each level
if current_number > 20:
break
print()
3) def remove_vowels(input_string):
# Define the vowels
vowels = "aeiouAEIOU"
# Use a list comprehension to filter out vowels from the string
result_string = ''.join([char for char in input_string if char not in vowels])
return result_string
Example usage
input_string = "guvi geeks network private limited"
result_string = remove_vowels(input_string)
print("Original string:", input_string)
print("String without vowels:", result_string)
4) def count_unique_characters(input_string):
# Use a set to store unique characters
unique_characters = set(input_string)
# Return the number of unique characters
return len(unique_characters)
Example usage
input_string = "guvi geeks network private limited"
unique_count = count_unique_characters(input_string)
print("Original string:", input_string)
print("Number of unique characters:", unique_count)
5)def is_palindrome(input_string):
# Remove any non-alphanumeric characters and convert to lowercase
cleaned_string = ''.join(char.lower() for char in input_string if char.isalnum())
# Check if the cleaned string is equal to its reverse
return cleaned_string == cleaned_string[::-1]
Example usage
input_string = "A man, a plan, a canal, Panama"
result = is_palindrome(input_string)
print(f"Is the string '{input_string}' a palindrome? {result}")
6) def longest_common_substring(str1, str2):
# Get the lengths of the strings
len1, len2 = len(str1), len(str2)
# Create a 2D list to store lengths of longest common suffixes
Initialize all values to 0
dp = [[0] * (len2 + 1) for _ in range(len1 + 1)]
Initialize variables to store the length of the longest common substring
and the ending index of the longest common substring in str1
longest_length = 0
end_index = 0
Build the dp array
for i in range(1, len1 + 1):
for j in range(1, len2 + 1):
if str1[i - 1] == str2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
if dp[i][j] > longest_length:
longest_length = dp[i][j]
end_index = i
Extract the longest common substring
longest_common_substr = str1[end_index - longest_length:end_index]
return longest_common_substr
Example usage
str1 = "guvi geeks network"
str2 = "geeks for geeks network"
result = longest_common_substring(str1, str2)
print("Longest common substring:", result)
7) def most_frequent_character(input_string):
# Create a dictionary to store the frequency of each character
frequency_dict = {}
# Iterate through each character in the string
for char in input_string:
if char in frequency_dict:
frequency_dict[char] += 1
else:
frequency_dict[char] = 1
Find the character with the maximum frequency
most_frequent_char = max(frequency_dict, key=frequency_dict.get)
return most_frequent_char
Example usage
input_string = "guvi geeks network private limited"
result = most_frequent_character(input_string)
print("Most frequent character:", result)
8)def are_anagrams(str1, str2):
# Remove any non-alphanumeric characters and convert to lowercase
cleaned_str1 = ''.join(char.lower() for char in str1 if char.isalnum())
cleaned_str2 = ''.join(char.lower() for char in str2 if char.isalnum())
# Sort the cleaned strings and compare
return sorted(cleaned_str1) == sorted(cleaned_str2)
Example usage
str1 = "Listen"
str2 = "Silent"
result = are_anagrams(str1, str2)
print(f"Are the strings '{str1}' and '{str2}' anagrams? {result}")
9)def count_words(input_string):
# Split the string into words based on whitespace
words = input_string.split()
# Return the number of words
return len(words)
Example usage
input_string = "guvi geeks network private limited"
word_count = count_words(input_string)
print(f"Number of words in the string: {word_count}")
Top comments (0)