Why?

Many years ago my Squash club asked me to generate a Round Robin schedule for 20 teams. This is a schedule where teams pair off in each round and after all of the rounds are played every team has played every other team just once. The Draw is the list of team pairs who play each other in each round.

How?

I was playing around with Prolog at the time so the OBVIOUS solution was to write a Prolog program to solve it. Prolog is a curious language which solves problems by being told what the problem is, how to go about solving it, and what the desired outcome is.

So I wrote the program which came to about 120 lines of code, set it in motion, and sat back while Prolog crunched through every conceivable combination until it found the first one that worked. This took a long time. A really long time on my 16MHz 80386.

Much later...

Fast forward 20 years and I have a job to do in Prolog, so it's time for a refresher. I drag out the old Round Robin program and tidy it up a bit to suit the newer Prolog standards. Without really altering the program logic it ran faster, but only in proportion to the computer's speed increase. Moving from the old 16MHz 80386 to my shiny new 4.7GHz i7 had a dramatic effect on how quickly I got an answer but it still left me with a serious problem.

Defeat

Due to the way Prolog worked the time to return a solution increased geometrically with the number of teams in the draw. Given enough teams, even the i7 was going to take until the end of the universe to finish. There had to be a better way.

Revelation

Just to avoid being defeated I sat down and analysed the problem properly, like I should have done in the first instance, and there before me appeared an analytical solution.

In fact the solution was so simple that the challenge was no longer to write a quicker program but to write the smallest program. As it turned out Python was perfectly suited to this task.

Solution

The resulting program is just a few lines of Python code, but the real work is just one line of code. As for speed, well it's no longer a consideration. It seems not to matter how many teams I choose I cannot get my finger off the ENTER button before the solution is found and displayed.

Back to school...

There is an important lesson in this tale. Take the time to study and analyse the problem before you begin creating your solution. Time spent in this part of your project is well spent and will avoid wasting lots of time later when you are committed to a particular design or solution.

Remember this. There is almost always a better way (to skin a cat!!).