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 = names_file.read().replace('"', '').split(',')
names_file.close()

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:

alphas = " ABCDEFGHIJKLMNOPQRSTUVWXYZ"
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.

Sunday 16 November 2008

Best laid plans and all that

So I mentioned previously the books I was looking at and I can inform you that I ended up with none of them! Whilst I was looking around we ended up going on holiday and I spent all of that lovely book money on wasteful stuff like having fun, what was I thinking. So to keep me going I started looking at a site run by a guy called Alex called Learn C++ which is not only informative but well written and I reckon is a good place to head over to whether you're an experienced programmer or complete novice.

Anyway in the end I picked up a book from Amazon as a reference by someone called Bjarne Stroustrup who happened to create the C++ language so I figured he must know what he's talking about. The book is called The C++ Programming Language: Third Edition, it was published a few years ago now but not a lot has changed really since. I haven't had a proper look through yet but when I do I'll let you know what it's like, currently I'm still working through the Learn C++ site.

The Python stuff has unfortunately taken a bit of back seat, I still haven't had a chance to work through the latest game in Linux Format as things have been busy and it's taking enough effort to fit in the C++ around it all. But hopefully as we get towards christmas things will quieten down a little and I can get back into it again.