Skip to content

Instantly share code, notes, and snippets.

@jstaffans
Created July 3, 2018 19:43
Show Gist options
  • Select an option

  • Save jstaffans/acf1ae90d2d2b939906a3eb876446809 to your computer and use it in GitHub Desktop.

Select an option

Save jstaffans/acf1ae90d2d2b939906a3eb876446809 to your computer and use it in GitHub Desktop.

Revisions

  1. jstaffans created this gist Jul 3, 2018.
    25 changes: 25 additions & 0 deletions the-test.clj
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,25 @@
    ;; 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))