By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
464,737 Members | 1,289 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 464,737 IT Pros & Developers. It's quick & easy.

Python least characters function

P: 4
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).
Apr 19 '13 #1
Share this Question
Share on Google+
2 Replies

bvdet
Expert Mod 2.5K+
P: 2,851
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:
Expand|Select|Wrap|Line Numbers
  1. ...snip...
  2.     words = sentence.lower().split()
  3.     least_characters = {}
  4.     for word in words:
  5.         least_characters.setdefault(vowel_count(word)/float(len(word)), []).append(word)
  6.     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:
Expand|Select|Wrap|Line Numbers
  1. len([letter for letter in word if letter in ['a', 'e', 'i', 'o', 'u']])
Apr 19 '13 #2

Expert 100+
P: 626
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.
Expand|Select|Wrap|Line Numbers
  1.     vowel = ['a','e','i','o','u']
  2.     words = sentence.lower().split()
  3.     least_characters = {}
  4.     for word in words:
  5.         for ltr in word:
  6.             if "a" <= ltr <= "z":  # omits everything else
  7.                 if ltr in vowel: 
Apr 19 '13 #3

Post your reply

Sign in to post your reply or Sign up for a free account.