Skip to content

Instantly share code, notes, and snippets.

@usametov
Created July 14, 2016 17:04
Show Gist options
  • Save usametov/1a9ceb8fa3ab1069750088d00ba3ad99 to your computer and use it in GitHub Desktop.
Save usametov/1a9ceb8fa3ab1069750088d00ba3ad99 to your computer and use it in GitHub Desktop.
4clojure solution to problem 82
(defn chk-ins [w1 w2]
(when (< (count w1) (count w2))
(some #(= w1 %)
(for [i (range (count w2))]
(str (subs w2 0 i) (subs w2 (inc i))))
)
))
(defn chk-subs [w1 w2]
(when (= (count w1) (count w2))
(some #(= (first %) (second %))
(for [i (range (count w1))]
[(str (subs w1 0 i) (subs w1 (inc i)))
(str (subs w2 0 i) (subs w2 (inc i)))]
))
))
(defn chk-del [w1 w2]
(when (> (count w1) (count w2))
(chk-ins w2 w1)))
(defn permutations [s]
(lazy-seq
(if (seq (rest s))
(apply concat (for [x s]
(map #(cons x %) (permutations (remove #{x} s)))))
[s])))
(defn is-connected [w1 w2]
(or (chk-ins w1 w2)
(chk-del w1 w2)
(chk-subs w1 w2)))
(defn word-chain [s]
(true? (some #(every? (fn[pair] (is-connected (first pair) (second pair))) %)
(map #(partition 2 1 %) (permutations s)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment