[PyAR2] Mini Programming Challenge

Coltrey Mather pyar2 at cowsgomoo.org
Thu Oct 16 13:28:55 CEST 2008


#!/usr/bin/env python
import sys
import string
from pprint import pprint

words = {}
chars = {}
letters = {}

for line in sys.stdin:
    for c in line:
        chars[c] = chars.setdefault(c, 0) + 1
        if c in string.letters:
            letters[c.lower()] = letters.setdefault(c.lower(), 0) + 1
    for word in line.split():
        words[word.lower()] = words.setdefault(word.lower(), 0) + 1

for x in (("Word Counts", words,),
          ("Character Counts", chars,),
          ("Letter Counts", letters,),):
    print(x[0])
    pprint(x[1])
    print


On Wed, Oct 15, 2008 at 17:53, Greg Lindstrom <gslindstrom at gmail.com> wrote:
> I'm not sure how many of you are new Python programmers, or Pythonistas,
> but here's a simple problem for you to work on.  Post you code to the list
> and we can see how everyone does it.  Here's the challenge (check out
> raw_input() for how to input a string):
>
> Write a program that prompts the user to enter a string of characters (it
> could be a word or a sentence or longer) and print out a report showing the
> number of times each letter appears in the word (or sentence).  I'm just
> concerned about letters, but I don't care about case (both upper and lower
> case count the same).
>
> I'll post mine in a couple days.
>
> -greg
>
> _______________________________________________
> PyAR2 mailing list
> PyAR2 at python.org
> http://mail.python.org/mailman/listinfo/pyar2
>
>


More information about the PyAR2 mailing list