Skip to content

Instantly share code, notes, and snippets.

@jstaffans
Created July 3, 2018 19:43
Show Gist options
  • Save jstaffans/acf1ae90d2d2b939906a3eb876446809 to your computer and use it in GitHub Desktop.
Save jstaffans/acf1ae90d2d2b939906a3eb876446809 to your computer and use it in GitHub Desktop.
The Test
;; https://hackernoon.com/how-to-lose-an-it-job-in-10-minutes-3d63213c8370
(defn set-rotated
"Returns a set of all possible rotations of a string"
[s]
(loop [i 0
acc #{}]
(if (= i (count s))
acc
(recur (inc i) (conj acc (->> s cycle (drop i) (take (count s)) (apply str)))))))
(defn group-rotated
"Groups strings so that each group contains the strings
that are rotations of one another."
[coll]
(->> coll
(map set-rotated)
(interleave coll)
(partition 2)
(reduce (fn
[acc [orig rotated-set]]
(let [found (get acc rotated-set [])]
(assoc acc rotated-set (conj found orig))))
{})
vals))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment