Skip to content

Instantly share code, notes, and snippets.

@jbranchaud
Last active January 6, 2016 23:28
Show Gist options
  • Save jbranchaud/6c40f53514281acefd9d to your computer and use it in GitHub Desktop.
Save jbranchaud/6c40f53514281acefd9d to your computer and use it in GitHub Desktop.

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

Clojure

(defn schedule-teams [teams]
  (map
    vec
    (distinct
      (remove nil?
        (for [x teams
              y teams]
          (when (not (= x y))
            (apply sorted-set [x y])))))))

(schedule-teams [:a :b :c])
([:a :b] [:a :c] [:b :c])

(schedule-teams [:a :b :c :d :e :f :g])
([:a :b] [:a :c] [:a :d] [:a :e] [:a :f] [:a :g] [:b :c] [:b :d] [:b :e] [:b :f] [:b :g] [:c :d] [:c :e] [:c :f] [:c :g] [:d :e] [:d :f] [:d :g] [:e :f] [:e :g] [:f :g]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment