Where is the mistake in my first function? I have this:
def fun(str):
for vowel in str:
if vowel in 'aeiouAEIOU':
return len(vowel)
def fun(str):
return len([vowel for vowel in str if vowel in 'aeiouAEIOU'])
print fun("halloo")
The results for the two functions are different. I must return the number of vowels the string contains.
4 Answers 4
In your first function you immediately return when you find a vowel, and you returned the length of that one vowel. The result is always either 1 (vowel found) or None (no vowel found).
If you wanted to count the number of vowels, you'd have to use a new variable to track that count:
def fun(str):
count = 0
for vowel in str:
if vowel in 'aeiouAEIOU':
count += 1
return count
The second function produces a list of all the vowels first, then takes the length of that list. You could use:
def fun(str):
return sum(1 for vowel in str if vowel in 'aeiouAEIOU')
to not even produce a list, just a vowel count.
Comments
In this function, when you reach a vowel, you're returning the length of vowel (a single character) - which is always going to be 1 (or if it drops off the end of the function None):
def fun(str):
for vowel in str:
if vowel in 'aeiouAEIOU':
return len(vowel)
While in this one, you're building up a list of all vowels and taking the length:
def fun(str):
return len([vowel for vowel in str if vowel in 'aeiouAEIOU'])
Note that str is a builtin type, it'd be better to call your parameter text to avoid any potential headaches (not necessarily in this function, but for future reference) in the future.
Ultimately, you can write this as (with a more descriptive name and parameter):
def vowel_count(text):
return sum(1 for ch in text.lower() if ch in 'aeiou')
Comments
Your first code will return 1 or None :
def fun(str):
for vowel in str:
if vowel in 'aeiouAEIOU':
return len(vowel)
print fun('111') # return None
print fun('abc') # return 1
Your Second code will be ok to return the number of char(s) in vowel.
Comments
Use count method of string. Time complexity is O(N) * 5
code:
def countVowel(input):
input = input.lower()
count = input.count("a") + input.count("e") + input.count("i") +\
input.count("o") + input.count("u")
return count
print "Result:- ", countVowel("halloo")
print "Result:- ", countVowel("halloo HELLOO A ")
output:
$ python test.py
Result:- 3
Result:- 7
By collections module. Time complexity is O(N) * 5
code:
import collections
def countVowel(input):
input = input.lower()
info = collections.Counter(input) #`O(N) * 5`
count = info["a"] + info["e"] + info["i"] + info["o"] + info["u"]
return count
print "Result:- ", countVowel("halloo")
print "Result:- ", countVowel("halloo HELLOO A ")
output:
$ python test.py
Result:- 3
Result:- 7
By regular expression
code:
import re
def countVowel(input):
input = input.lower()
count = len(re.findall("a|e|i|o|u", input))
return count
print "Result:- ", countVowel("halloo")
print "Result:- ", countVowel("halloo HELLOO A ")
output:
$ python test.py
Result:- 3
Result:- 7
Time Complexity O(N)*1
code:
def countVowel(input):
input = input.lower()
count = 0
for i in input:
if "aeiou".__contains__(i):
count += 1
return count
print "Result:- ", countVowel("halloo")
print "Result:- ", countVowel("halloo HELLOO A ")
output:
$ python test.py
Result:- 3
Result:- 7
3 Comments
count does..O(n)? I mean, n*5 is still O(n)
vowel, as it isn't necessarily one. I'd go withcharacterinstead. Obviously that doesn't change anything about the problem itself, but there are plenty of answers to that effect already.