Skip to content

Instantly share code, notes, and snippets.

@uvtc
Created August 28, 2012 04:46
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 uvtc/3495031 to your computer and use it in GitHub Desktop.
Save uvtc/3495031 to your computer and use it in GitHub Desktop.
classroom distribution
;; 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