Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created October 12, 2013 23:22
Show Gist options
  • Save dchelimsky/6956098 to your computer and use it in GitHub Desktop.
Save dchelimsky/6956098 to your computer and use it in GitHub Desktop.
(defn chainable?
([words]
((complement not-any?) #(chainable? % (clojure.set/difference words %)) words))
([word others]
(letfn [(levenshtein [str1 str2]
(cond (empty? str1) (count str2)
(empty? str2) (count str1)
:else
(let [cost (if (= (first str1) (first str2)) 0 1)]
(min (inc (levenshtein (rest str1) str2))
(inc (levenshtein str1 (rest str2)))
(+ cost (levenshtein (rest str1) (rest str2)))))))]
(let [candidates (set (filter #(= 1 (levenshtein word %)) others))]
(cond (empty? others) true
(empty? candidates) false
:else
(some #(chainable? % (clojure.set/difference others #{%})) candidates))))))
@mattwynne
Copy link

Wow. I can't quite read this but I look forward to you explaining it to me someday. I'm impressed you cracked it!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment