Thursday, December 24, 2009

Python Fun

Every programmer, when learning something new, hits on that devilish quandary:

"What should I program?"

And I am a man like all others, who twiddles my thumbs and wonders "ah... if only I had something to /do/ with this language, I would really start to learn it!"

Well, I've decided to just dink around uselessly and see what comes of it. Then share.

The things we'll cover:
  • Some stupid string stuff
  • some more stupid string stuff, with numbers involved
  • then some regular old math

stupid string stuff:

First I started off playing with the string and list functions, and interchanging between them. Throughout this, I might duplicate existing functionality -- if I do, comment and tell me how to do it better! I'd love to learn.

I decied that I wanted to take a string, say "Hey man, what's up?" and insert stuff between each letter. I couldn't really find a quick way to do this, but some related things popped up.
a = "Hey man, what's up?"
will give you ['Hey', 'man', 'what\'s', 'up?'] - and that's nice, but I want things broken down to the letter. So I wrote a function breakout:
def breakout(input):
r = []
for i in input:

return r
breakout("hello") returns ['h','e','l','l','o']

then you can do a str.join() - let's wrap it into a function!
def splitByChar(instring, splitchar):
return splitchar.join(breakout(instring)
you can, through this, call splitByChar("hello", '.') and receive 'h.e.l.l.o'

ah, stupid pointless string stuff.

more stupid string stuff, with some math

Next up, I decided to play with phone numbers. I decided to break them up into their constituent digits and add them. This was (as many things in python are), an easy task.
def summit(input):
sum = 0
for i in input:
if i.isdigit():
sum += int(i)

return sum
This function runs quickly through the string and adds to the sum for every integer found.

I put in my phone number, print(summit("519-703-3336")), and received 42. Exciting!

Then I got to wondering.... I wonder how many phone numbers out there sum to 42? I wonder what the distribution is like across the rang of sums from 0 to 90? Let's write some code to find out:
def smattering(input):
arr = []
for i in range(0,9*input):

temp = []
for i in range(0,input):

upper = int(''.join(temp))
upperStr = str(upper)
for i in range(0, upper):
arr[summit(str(i))] += 1
if i % 1000000 == 0:
print(str(i) + ", " + str(upper - i) + ", " + str(i/upper))

return arr
This code makes a 90-element list, then brute-force walks from 0 to 9,999,999,999 and calculates every single sum, stopping every million steps to print out how far we are in, how far we have left, and a percentage through the path we've walked. I did some simple calculations, every printout occurs about 9 seconds apart, and 9,999 of them are required to take the calculation to completion.

This translates to about 24.9 hours. That's a pretty long calculation -- and it's kind of required; summing each of 10 digits for each of 10 billion numbers is 100 billion operations no matter what way you cut it. I've left the calculation running in a terminal (two, actually - one which will conclude and print the values to the screen and to a text file, and another in an interactive terminal in case I come up with fun things I want to do to the list off the bat) - but I'm beginning to think

some regular old math

is in order to find out the answer to this question a bit faster. The clock is ticking, and I have a day to find out. I'll update once I've thought about it a bit.

1 comment:

edoules said...

A breakout function using list comprehension instead:

def breakout(input):
return [i for i in input]

I followed a link here from Tony Thompson's blog :D