1

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.

Jonathan Hartley
16.2k9 gold badges84 silver badges81 bronze badges
asked Jan 29, 2015 at 12:05
1
  • On a side note, I would not call the loop-variable vowel, as it isn't necessarily one. I'd go with character instead. Obviously that doesn't change anything about the problem itself, but there are plenty of answers to that effect already. Commented Jan 29, 2015 at 12:18

4 Answers 4

3

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.

answered Jan 29, 2015 at 12:07
Sign up to request clarification or add additional context in comments.

Comments

2

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')
answered Jan 29, 2015 at 12:07

Comments

1

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.

answered Jan 29, 2015 at 12:10

Comments

0

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
answered Jan 29, 2015 at 12:12

3 Comments

O(N) * 5 complexity - ouch!?
yes, just showing OP that this can be other way. So he can understand what count does..
From a complexity standpoint: Aren't both algorithms (the other one being testing each char if it is a vowel) O(n)? I mean, n*5 is still O(n)

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.