Skip to content

Instantly share code, notes, and snippets.

@jwworth
Last active January 10, 2016 01:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jwworth/5b9d80f514a15efc780d to your computer and use it in GitHub Desktop.
Save jwworth/5b9d80f514a15efc780d to your computer and use it in GitHub Desktop.
Round Robin - Lua
--[[
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.
--]]
-- Solution (Lua)
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
end
table.remove(teams, 1)
end
print(count .. " games\n")
end
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment