-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcount_vowels.py
80 lines (60 loc) · 1.43 KB
/
count_vowels.py
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
"""
Problem
-------
**Count Vowels**
Enter a string and the program counts
the number of vowels in the text.
For added complexity have it report a
sum of each vowel found.
Solution
--------
The solution is provided using a lambda function
Author
------
dbonadiman
"""
import sys
def vowels(s):
"""
vowels
This solution uses a lambda function that
sums one if the first letter of the word is a vowel,
zero othrerwise and then recursively on the rest of the word.
There is a funny optimisation using the fact that in
python boolean and integer are actually the same thing
so we actually sum the condition instead of extensively
write
if condition:
return A+1
else:
return A
we use this compact form
return A+condition
Parameters:
s ==> the string from wich to count the vowels
Test:
>>> vowels('ciao')
3
>>> vowels('cat')
1
>>> vowels('mmm')
0
"""
#Python is fuking awesome
i = lambda s: i(s[1:])+(s[0] in "aeiou") if len(s) else 0
return i(s)
def main():
try:
print("\nThis program counts the vowels in a string\n"
"Please enter the string: \n")
string = raw_input("--> ")
print(vowels(string))
return 0
except Exception, e:
print(e)
return 1
if __name__ == "__main__":
import doctest
doctest.testmod()
status = main()
sys.exit(status)