[PyAR2] Fwd: Mini Programming Challenge

Larry Hale pythonlarry at gmail.com
Tue Oct 21 05:04:38 CEST 2008


Sorry, Greg; looks like "with lack of sleep comes replying to the wrong
entity..."  :>


---------- Forwarded message ----------
From: Larry Hale <pythonlarry at gmail.com>
Date: Wed, Oct 15, 2008 at 11:22 PM
Subject: Re: [PyAR2] Mini Programming Challenge
To: Greg Lindstrom <gslindstrom at gmail.com>


# I'm not new, but thought I'd give my initial attempt - with lack of sleep
comes verbosity??  :)

# (Besides, it's always fun to see the
more-elegant-than-mine/simply-different solutions to "simple" problems...
:)


from string import lowercase as lowercase_letters

user_input = raw_input( 'Lay a string on me: ' ).lower()

list_of_chars = list( user_input )

dict_of_chars = {}

for c in list_of_chars:
    if c in lowercase_letters:
        if dict_of_chars.has_key( c ):
            dict_of_chars[ c ] += 1
        else:
            dict_of_chars[ c ]  = 1

print
print
print 'Letters used:'
print

list_of_chars = dict_of_chars.keys()

for c in list_of_chars:
    print '\t"%s" used %s time%s' % ( c, dict_of_chars[ c ], { True: 's',
False: '' }[ dict_of_chars[ c ] != 1 ] )

print
print
raw_input( 'Captain Irony Says: "Press [ENTER] to exit..."' )

# more simply...

from string import lowercase as lowercase_letters

user_input = raw_input( 'Lay a string on me: ' ).lower()

print
print
print 'Letters used:'
print

for c in lowercase_letters:
    count = user_input.count( c )

    if count:
        print '\t"%s" used %s time%s' % ( c, count, { True: 's', False: ''
}[ count != 1 ] )

print
print
raw_input( 'Captain Irony Says: "Press [ENTER] to exit..."' )



# ---------------------


On Wed, Oct 15, 2008 at 5:53 PM, 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
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20081020/0d1ecdae/attachment.htm>


More information about the PyAR2 mailing list