Created
August 28, 2012 04:46
-
-
Save uvtc/3495031 to your computer and use it in GitHub Desktop.
classroom distribution
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
;; I want `classrooms` (the accumulator) to end up looking like this: | |
;; {"A" #{"a" "d" "g" "j"} | |
;; "B" #{"b" "e" "h"} | |
;; "C" #{"c" "f" "i"}} | |
(defn distribute-students | |
[students teachers] | |
(let [classrooms (into {} (for [t teachers] [t #{}]))] | |
(loop [student-pool students ;; We'll pare this down as we go. | |
classrooms classrooms ;; We'll build this up as we go. | |
[curr-teacher & more] (cycle teachers)] ;; | |
(if (seq student-pool) | |
;; There are still students that need to be assigned to a class. | |
(recur (rest student-pool) | |
(update-in classrooms [curr-teacher] conj (first student-pool)) | |
more) | |
;; otherwise, all students have been assigned to a classroom. | |
classrooms)))) | |
(println (distribute-students | |
["a" "b" "c" "d" "e" "f" "g" "h" "i" "j"] | |
["A" "B" "C"])) | |
;; output: | |
; {A #{a d g j}, B #{b e h}, C #{c f i}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment