Created
July 3, 2018 19:43
-
-
Save jstaffans/acf1ae90d2d2b939906a3eb876446809 to your computer and use it in GitHub Desktop.
The Test
This file contains hidden or 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
;; 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