Skip to content

Commit a9fbe49

Browse files
bites 132 - refactored
1 parent 0201225 commit a9fbe49

File tree

1 file changed

+10
-17
lines changed

1 file changed

+10
-17
lines changed

132/vowels.py

+10-17
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
11
VOWELS = list('aeiou')
2-
text="""Python is an easy to learn, powerful programming language."
2+
text = """Python is an easy to learn, powerful programming language."
33
"It has efficient high-level data structures and a simple "
44
"but effective approach to object-oriented programming. "
55
"Python’s elegant syntax and dynamic typing, together with "
66
"its interpreted nature, make it an ideal language for "
77
"scripting and rapid application development in many areas "
88
"on most platforms."""
99

10+
11+
def count_vowels(word):
12+
return len([v for v in word if v in VOWELS])
13+
14+
1015
def get_word_max_vowels(text):
1116
"""Get the case insensitive word in text that has most vowels.
1217
Return a tuple of the matching word and the vowel count, e.g.
1318
('object-oriented', 6)"""
1419
# print(type(text))
15-
words = []
16-
for line in text.splitlines():
17-
line = [word.lower() for word in line.split(" ") if len(word) > 0]
18-
words.append(line)
19-
listofwords = []
20-
for list in words:
21-
listofwords += list
22-
result = []
23-
for word in listofwords:
24-
point = 0
25-
for ch in word:
26-
if ch in VOWELS:
27-
point+=1
28-
result.append((word,point))
29-
result.sort(key=lambda x:x[1])
30-
return result[-1]
20+
words = text.lower().split(" ")
21+
words = [(word, count_vowels(word)) for word in words]
22+
return max(words,key=lambda x: x[1])
23+
3124

3225
print(get_word_max_vowels(text))

0 commit comments

Comments
 (0)