Question:
GIVEN some strings in that we need to print
The strings which does not contain more than one vowels in it take list of strings as input and create a function vowelString() that will return list of strings where strings have one or no vowels in it.
Input:
5
hello
world
python
apple
banana
Output:
hello
world
python
Answer:
def vowelString(lis):
st = 'aeiou'
vowel_lis = []
for str in lis:
count = 0
for let in str:
if let in st:
count = count + 1
if count <= 1:
vowel_lis.append(str)
return vowel_lis
lis = []
for _ in range(int(input())):
lis.append(input())
for i in vowelString(lis):
print(i)
Code Explanation:
This is a Python code for a function vowelString() that takes a list of strings lis as an input and returns a list of strings containing only those strings from the input list that have at most one vowel.
Here's how the code works:
The function vowelString() first initializes a string variable st with the string "aeiou", which represents the vowels in English language.
It then initializes an empty list vowel_lis that will be used to store the strings that have at most one vowel.
The function then iterates through each string in the input list lis.
For each string, it initializes a variable count to 0, which will be used to count the number of vowels in the string.
It then iterates through each character in the string using a for loop.
If the current character is a vowel (i.e., if it is present in the string st), it increments the count variable.
After the for loop, if the count variable is less than or equal to 1, the string is added to the vowel_lis list.
Finally, the function returns the vowel_lis list containing only those strings that have at most one vowel.
The code then prompts the user to enter the number of strings they want to input and then takes that number of strings as input and adds them to the list lis.
It then calls the vowelString() function with the lis list as an argument and iterates through the returned list to print each string on a new line.
Overall, this code filters out the strings that have at most one vowel and prints them.
Comments
Post a Comment