[PyAR2] Programming Challenge II - One is the Magic Number
Gordon Fisher
phi3d at yahoo.com
Tue Oct 28 03:18:23 CET 2008
Here's my entry. It's your basic brute force model. I knew that others would find a more elegant way to do it, so I decided to make a generalized version that could count any digit from any number.
#Gordon Fisher Oct 27, 2008
print "This program counts the number of times a single digit appears in a series of numbers from 0 to a selected number."
Xstring = raw_input("Please enter a Number then press Enter, do not include commas: ")
keynumstring = raw_input("Please enter a number from 0-9 that you wish to count then press Enter,: ")
magicnum = int(Xstring)
total = 0
xlen = 0
for x in range(magicnum +1):
Xstring = str(x)
xlen = len(Xstring)
stringtotal = Xstring.count(keynumstring,0,xlen)
total = total + stringtotal
print "The number " + str(keynumstring) + " appears " + str(total) + " times."
Gordon
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/mailman/private/pyar2/attachments/20081027/99698be1/attachment.htm>
More information about the PyAR2
mailing list