Generally you are on the right track, but I think you have some problems with your algorithm. What you want is to create a dict with the frequencies as keys and words as values. First, I would create a function that returns the number of vowels in a word. Your dictionary values should be lists so your result would include words that have the same vowel proportion.
dict method setdefault is ideal for this. Return the value of
dict[min(dict)] since that is the answer you are looking for.
Something like:
- ...snip...
-
words = sentence.lower().split()
-
least_characters = {}
-
for word in words:
-
least_characters.setdefault(vowel_count(word)/float(len(word)), []).append(word)
-
return least_characters[min(least_characters)]
I change some of the identifier names so it would be more intuitive.
vowel_count is the function returning the number of vowels in a word. That could be reduced to a one-line list comprehension - possibly:
- len([letter for letter in word if letter in ['a', 'e', 'i', 'o', 'u']])