Created
October 12, 2013 23:22
-
-
Save dchelimsky/6956098 to your computer and use it in GitHub Desktop.
Solution to http://www.4clojure.com/problem/82
This file contains 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
(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)))))) |
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
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.