Fun with pythonchallenge.com

This site is ultra cool. I should be studying for my exams, but this is bloody addictive: http://pythonchallenge.com

Anyway, I made it to the 10th level where you can see a bull saying: len(a[30]) = ? so obviously i need to calculate the length of the 31st member of a. Now, we don't know what a is, but fortunately, the kaw does. Clicking on it will give you this:

a = [1, 11, 21, 1211, 111221,

A sequence of some sort ... i spent like half an hour trying to decipher it and it didn't make any sense, so I did what every sane person would do: consult the On-line encyclopedia of integer sequences and it turns out it's the look-and-say sequence. How was I supposed to know that? It goes like this: first we have ONE one (1), so we say it: ONE ONE. That's 11. Now we have TWO ones so that's 21. Then we have ONE Two and ONE One (1211). Aham... So to cut things short, here's what I wrote to calculate the length of the 30th member:


def make_look_and_say( members = 1 ):
  seq = ["1"]
  for x in range(0,members):
    new_member = ""
    char = seq[-1][0]
    streak = 1
    for c in seq[-1][1:]:
      if c == char:
        streak += 1
      else:
        new_member += str(streak) + char
        streak = 1
        char = c
    else:
      new_member += str(streak) + char
    seq.append( new_member )
  return seq

print len(make_look_and_say(31)[30])

Now I'm stuck at level 11 and it seems that there is some image that has pixels interpolated or extrapolated, but I really cannot guess what it is. I suppose I'll read up on image libraries some other time.

Anyway, the challenge is awesome, you should try it. There really is only one python specific question (involving pickles) and everything else can be solved (up to level 10 of course :) with any other programming language .

Leave a Reply