Skip to content

Instantly share code, notes, and snippets.

@dchelimsky
Created October 12, 2013 23:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • 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))))))
@dchelimsky
Copy link
Author

Full disclojure (see what I did there?): the levenshtein fn came from the internet: http://rosettacode.org/wiki/Levenshtein_distance. The rest is mine, for better or worse.

@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