Problem of the Day
A new programming or logic puzzle every Mon-Fri

Round Robin Scheduler

Welcome to the first week of June!

Today's problem is a fun one. Your goal is to create a round robin tournament scheduler. That is, given an array of 8 teams or more figure out a way to, as evenly as possible, divide them in to divisions of 3, 4, or 5 teams. Then you'll need to create a schedule for each group so that each team plays each other exactly one time. Here is an example:

['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']
Division 1
A vs B
C vs D
A vs C
B vs D
D vs A
B vs C

Division 2
E vs F
G vs H
E vs G
F vs H
H vs E
F vs G

Permalink: http://problemotd.com/problem/round-robin-scheduler/

Comments:

  • Anonymous - 10 years, 6 months ago

    Finally first! Python 2.7.

    def divide(sets):
        for i in range(2, 5):
            if len(sets) % i == 0:
                return i
        return 2
    
    def permute2(end):
        while (len(end) > 2):
            piv = end[0]
            for i in range(1, len(end)):
                print piv + " vs " + end[i]
            end.pop(0)
        print end[0] + " vs " + end[1]
    
    def schedule(sets):
        sep = divide(sets)
        pivot = len(sets) / sep
        j = 0
        for i in range(sep):
            print "Division " + str(i + 1)
            permute2(sets[j:j+pivot])
            j += pivot
    

    reply permalink

Content curated by @MaxBurstein