Saturday, 29 November 2008

And that's why I love python!

Well for the last few weeks I've been hacking away with C++ whenever I get some free time which although interesting has been tedious, after all having to remember when you're passing pointers, references or values gets to be a pain real quick.

One of the really nice things with something like C++ though is memory management and speed when you get it right, you're working with natively compiled code and not having to wait for a garbage collector to clear up your allocations for you which makes for nice neat code. Of course like I said this is when you get it right, the downside is getting it wrong means memory leaks, stack overflows, variables pointing to garbage and so on which is troublesome to fix.

Today I got a little bored of all this so I figured it was time to put my Python hat back on and do some coding which was a little more interesting. So I took up one of the puzzles over at Project Euler (22 to be exact) which involves reading in a file of over 5000 names, sorting them alphabetically and then adding the numerical value of each name multiplies by it's position to an overall count and deducing what the total value of the names is. So where "COLIN" is at position 938 and it's numerical value is 53 (C=3, O=15 etc...) it's overall value is 49741.

I dread to think how to do this in C++ but in python it's a breeze thanks to it's list comprehension and easy file methods. So reading in the file is simply:

names_file = open("names.txt", "r")
names ='"', '').split(',')

And there we have the file being opened, the contents being read, having any quotes removed and then split by commas to get a list and finally the file being closed. Then to do something like figure out the numerical value of COLIN we do something like this:

name_value = sum([alphas.index[c] for c in "COLIN"])

so alphas is defining a string (with an intentional first space so that A is now at index 1), the alphas.index[c] part basically returns the index in the alphas string of a character and the "for c in "COLIN"" part is looping through each letter in the name. Surrounded by the square brackets means we are producing a list, so each iteration adds a new value to the list and then the sum() adds them all together. It's just to easy.

If anyone is interested, the finished code (not published here as I shall leave it as an exercise for the reader to complete) takes about 92 milliseconds to run against all 5163 names.


  1. Never in a thousand years, or at least the last 15, would I have thought to start a string with a space due to issues involving zero based indexes of a string. My first reaction was to question your mental state: That's Crazy! However, in retrospection, I find it to be a very elegant solution. In fact, it appears to be the easiest solution to incrementing the index of a string.

    As a side note, if you are looking to experiment with a compiled language with memory management, that is similar in syntax to python, I would suggest checking out Genie

  2. No you were right to question my mental state, to be honest I wasn't sure initially myself when doing that but it worked out quite nicely and means that I have less to do in later functions.

    Thanks for the reference, I'm trying my hardest not to say "I'll have to check that out" :)

    (For anyone who doesn't get that last reference check out the Linux Outlaws podcast)

  3. I find writing in plain C is simpler than C++ i think alot of people get into a hype with C++ but i guess it does make holding data much easier. But i prefer the clean approach with C been using it way more this year, so yeah i guess you should give it ago! But its preference and depends what you need and want like python is perfect for the Euler projects there because its so high level it makes it easy to make crazy stuff with the data but yeah hehe

    Keep the blog going just found it :)

  4. I like the OO aspect of C++ and it does make some things easier, given the choice though I'd rather not now it at all :)

    I know someone I work with implemented a Project Euler solution in both C++ and Python with the C++ solution running about 3 times faster, so like with most things it's picking the right tool for the job.

    Thanks for the comment by the way, it's what encouraged the latest post.


Note: only a member of this blog may post a comment.