Reputation: 173
I want to find strings with maximum vowels in Python, but I don't know how.
Input
['Zak6aa','kak','ll','ak']
Output:
['Zak6aa']
Another if two or more strings tie for max vowels:
['Zaa', 'bii','ak']
output is:
['Zaa', 'bii']
My try:
def maxv(string):
sam=0
seq=[]
for i in range(len(string)):
k=0
v=0
for char in string[i]:
if char in "aeiouy":
v = v+1
k=(len(string[i]))-v
if (v >=k):
seq=seq+[string[i]]
return seq
Upvotes: 0
Views: 2453
Reputation: 1
I tried to solve this with lower complexity:
sample_string="i am a sample string find word with maximum vowel"
length=len(sample_string)
count=0
max_count=0
word=""
max_word=""
for i in range(length):
if(sample_string[i].isspace()):
if(count>max_count):
max_count=count
max_word=word
count=0
word=""
elif(sample_string[i]=='a' or sample_string[i]=='e' or
sample_string[i]=='i' or sample_string[i]=='o' or
sample_string[i]=='u'):
count=count+1
word=word+sample_string[i]
print(max_word)
Upvotes: 0
Reputation: 173
Thanks everybody
How to print the number of swaps in selection sort in Python? For example output `
[1, 2, 3, 4, 5, 6, 7, 9, 8]
` input
1
because there is 1 swap 9 with 8.
def min(list, fromw):
m = fromw
for i in range(fromw + 1,len(list)):
if list[i] < list[m]:
m = i
return m
def select(list):
for promt in range(len(list)):
where = min(list, promt)
list[where], list[promt] = list[promt], list[where]
return list
Upvotes: 0
Reputation: 42756
You can sort
them by the number of vowels:
l = ['Zak6aa','kak','ll','ak']
sorted(l, key=lambda x: sum(1 for y in x if y in 'aeiouAEIOU'), reverse=True)
['Zak6aa', 'kak', 'ak', 'll']
If you need all with the same max lenght, you can use zip with the number of vowels before sorting and then just filter the higher "scores":
l = ['Zaa', 'bii','ak']
slist =sorted(zip(map(lambda x: sum(1 for y in x if y in 'aeiouAEIOU'), l), l), reverse=True)
filter(lambda x: x[0] == slist[0][0], slist)
[(2, 'bii'), (2, 'Zaa')]
Upvotes: 1
Reputation: 1672
One Solution using loops.
l=['Zak6aa','kak','ll','ak']
L={}
for x in l:
cnt=x.count('a')+x.count('e')+x.count('i')+x.count('e')+x.count('u')
if cnt not in L.keys():
L[cnt]=[]
L[cnt].append(x)
print(L[max(L.keys())])
You can count the number of occurrences of vowels using re
import re
cnt=len(re.findall(r'[aeiou]{1}',x))
Upvotes: 1