Jake Worth

Note: any code contained in this post is more than a year old. Please use at your own risk.

A while ago, I solved the following programming challenge:

Round Robin: Given 3 Teams (A, B, C), we want to organize a tournament schedule such that every team plays every other team exactly once. Here is a valid schedule for these 3 teams:

A - B

B - C

A - C

How about if we have N teams? Devise a general purpose algorithm that generates tournament schedules for N teams.

My solution is below.

This was the first program I ever wrote in Lua, so it's tough to constructively criticize it. I was (and am) still in the 'it works, ship it' phase with this language.

That said, I like that it's a reusable, functional method with no side effects. I like that it has opinions about the scope of each variable. I like that is presents the output in a readable way.

I'm not a huge fan of how my solution telegraphs my implementation. Because of how it's written, the output doesn't resemble an actual schedule in any way. Though beyond the scope of this assignment, that wouldn't be good enough for production code.

function round_robin(...)
  local teams = {...}
  local count = 0

  while #teams > 1 do
    for i = 1, #teams - 1 do
      print(teams[1] .. ' vs. ' .. teams[i + 1])
      count = count + 1
    table.remove(teams, 1)
  print(count .. " games\n")

round_robin('Denver', 'New England')
round_robin('Denver', 'New England', 'Jacksonville')
round_robin('Denver', 'New England', 'Jacksonville', 'Green Bay', 'San Diego')

-- Denver vs. New England
-- 1 games

-- Denver vs. New England
-- Denver vs. Jacksonville
-- New England vs. Jacksonville
-- 3 games

-- Denver vs. New England
-- Denver vs. Jacksonville
-- Denver vs. Green Bay
-- Denver vs. San Diego
-- New England vs. Jacksonville
-- New England vs. Green Bay
-- New England vs. San Diego
-- Jacksonville vs. Green Bay
-- Jacksonville vs. San Diego
-- Green Bay vs. San Diego
-- 10 games


Thanks Josh Branchaud for posting this challenge.

Mar 6, 2016

Hi! I'm Jake Worth, a developer at Hashrocket, based in Chicago. I co-organize Vim Chicago. Read my blog, learn about my work, follow me on Twitter and Github, get in touch.