Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

> Or write a program which takes this post as input and tells me what the 3rd most common letter used was.

I'm going for 'a'.

And, according to http://en.wikipedia.org/wiki/File:English_letter_frequency_(... (via http://en.wikipedia.org/wiki/Letter_frequency) your four most frequently used letters match the table for English.

Tested in Python 2.7:

  s = "your text here"

  import string

  d = {}

  for i in list(s.lower()):
    if i in string.letters:
      d[i] = d.setdefault(i, 0) + 1

  print sorted(d.keys(), cmp = lambda x, y : cmp(d[x], d[y]), reverse = True)[2]
Edit: See my later response for something using the 'collections' module.


You don't need to do `list(s.lower())`, you can already iterate over a string. Also, instead of converting the whole string to a new lowercase one, which means allocating "a lot" of memory, you can convert each character separately as needed.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: