Skip to content

Instantly share code, notes, and snippets.

@jwworth jwworth/round-robin.lua
Last active Jan 10, 2016

Embed
What would you like to do?
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
You can’t perform that action at this time.