I have to write a function which takes one argument text containing a block of text in the form of a str, and returns an alphabetically sorted list of word(s) with the lowest “vowel proportion” as a list. The vowel proportion is defined as the proportion of letters in the word which are vowels (e.g. the vowel proportion of "life" is 2 = 0.5).
Python least characters function
Collapse
X
-
Tags: None
-
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:
Code:...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:Code:len([letter for letter in word if letter in ['a', 'e', 'i', 'o', 'u']])
-
You punc_char substitution is bass-ackwards in that an "&" will be included because it is not in your list. To add to bvdet's post, and this is a little too much nesting for my tastes so a function(s) should be included, perhaps to process each word individually, to reduce the nesting and improve readability.Code:vowel = ['a','e','i','o','u'] words = sentence.lower().split() least_characters = {} for word in words: for ltr in word: if "a" <= ltr <= "z": # omits everything else if ltr in vowel:Comment
Comment