Solving a math puzzle with Python’s generator expressions

Recently James Grime of numberphile-fame posted a puzzle on YouTube. The video is right here but the question is (if you don’t care to watch): „How many ways are there to completely fill a Noughts and Crosses (tic-tac-toe) board, with four noughts and five crosses? Not including rotations and reflections.“

So I am not much of a mathematician and I also did not really care for fopping about much so I wrote a small program in python to find the solution. And I took the chance to make a post about generator expressions in python as well.

A generator expression can take two general forms:

def gen(count):
  c = 10
  while c > 0:
    yield c
    c = c - 1

Here the yield keyword is what it’s all about. This will generate an iterable object that returns the yielded items. The cool thing is, that computation of this method is suspended until the next item is needed.

If you already have an iterable you can create a new generator expression from it:

g = (x for x in gen(10))

Here you can filter or map the value of x as you will see. And again, this is only a generator. No computation happens unless you actually want to print any values or you manifest them into a list.

you will find the source code of my solution below the fold


Warum dopen wir nicht alle?

Jetzt ist es raus. Armstrong hat gedopt. Jetzt werden natürlich erste Stimmen laut, dass die Mondlandung damit für ungültig erklärt wird, aber da glaube ich nicht dran.

Armstrong hatte Krebs in der Lunge und hat einen seiner Hoden an den Krebs verloren. Und mit Doping wird er der leistungsfähigste Sportler der Welt. Das sind unsere großen Vorbilder.

Taking more of the same too

There are more films coming where a bad actor’s daughter gets taken.

But this one seems like it might be fun. I think Stalone is way too bad an actor to take himself very seriously and I like the idea that he does what needs to be done and the cop with him can do nothing because he needs it done as well.

I see lots of comedic potential. Will make for an average but lighthearted movie. I approve.

Taking more of the same

There’s a new movie with Nicolas Cage.

I think Nicolas Cage might have a particular set of skills. For example: having only two facial expressions which sets this film up to be hilariously bad. But the craziest thing about this trailer must be the ridiculous 90s-style narrator telling us the story of how Liam Neeson’s daugther gets taken (or that is what you could easily assume, if you weren’t watching the incredibly bad performance Cage displays – even in this cut-together piece of cinematic non-history).

This might turn out to be worse than Wicker Man.