[Tutor] THE MAIL KING - ah13

Danny Yoo dyoo@hkn.eecs.berkeley.edu
Thu, 4 Oct 2001 09:41:50 -0700 (PDT)


On Thu, 4 Oct 2001, CC Computer Consulting Co. wrote:

> Make Every Letter Count - Get Every Letter Opened!

You've Got Things Reversed And You Have Things Cased.  This is a common
mistake if you're a newbie to the English language, so we'll forgive you
for now.  This is Tutor though; we cannot help you learn about word order
and the proper role of capitalization.

We can help you with Python though.  You probably meant to say "Make a
count of every letter":

###
def histogram(thing):
    counts = {}
    for element in thing:
        counts[element] = counts.get(element, 0) + 1
    return counts
###


This function is pretty useful if you're trying to make sense out of
"nonsense" --- cryptographers often use histograms to drive their guesses
of what a message really contains.


Here's an example that shows how this histogram function works:

###
>>> histogram("> Make Every Letter Count - Get Every Letter Opened!")
{'>': 1, 'y': 2, 'v': 2, 't': 6, 'u': 1, 'r': 4, 'p': 1, 'n': 2, 'o': 1,
'-': 1, 'k': 1, 'a': 1, 'd': 1, 'e': 10, ' ': 9, '!': 1, 'O': 1, 'L': 2,
'M': 1, 'G': 1, 'E': 2, 'C': 1}
###


There.  Sense out of nonsense.