## No more tricks

Posted by – February 11, 2009

Programming used to have a lot to do with little puzzles and tricks; pointer arithmetic, bitmasks and logical operators, writing to video memory etc. Someone once asked me how do you swap the contents of two (numerical) variables without using a third one in between (of course you’d never want to do this anyway, but it’s a “fun” question). The answer is something like this:

a = a+b
b = b-a
a = a+b
b = -1*b

This is how you do it in Python:

a, b = b, a

New-fangled programming languages, eh? (Ok, Python was first released in 1991 which was a long time before I programmed anything, but anyway.)

Apropos of that last notation, this is how you write Euclid’s algorithm in Python (% is the modulo operator):

def euclid(a,b):
while b:
a, b = b, a%b
return a

It’s sickeningly succinct, really. I kind of quite like it, but it kind of feels not very hardcore (comparing to, say, The Story of Mel which I’ve linked to before). Although really it’s just normality by now. Meh, meh.

What makes a man turn neutral? Lust for gold? Power? Or were you just born with a heart full of neutrality?