Last active
August 29, 2015 13:59
-
-
Save dyerw/10787247 to your computer and use it in GitHub Desktop.
finds all anagrams in a text file
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 get-words-from-file [file-name] | |
(into [] (re-seq #"\b[a-zA-Z]+\b" (slurp file-name)))) | |
(defn is-anagram? [word1 word2] | |
(let [word1-letters (seq word1) | |
word2-letters (seq word2)] | |
(= (sort word1-letters) (sort word2-letters)))) | |
(defn get-ana-set [word other-words] | |
(loop [words other-words | |
result #{word}] | |
(cond (empty? words) result | |
(is-anagram? word (first words)) (recur (rest words) | |
(conj result (first words))) | |
:else (recur (rest words) | |
result)))) | |
(defn anafind [words] | |
(loop [word (first words) | |
other-words (rest words) | |
result #{}] | |
(if (empty? other-words) result | |
(let [anagram-set (get-ana-set word other-words) | |
;; Everything we just put into the set needs to be taken out of the list | |
new-list (into [] (clojure.set/difference (into #{} other-words) | |
anagram-set))] | |
(recur (first new-list) | |
(rest new-list) | |
(conj result anagram-set)))))) | |
(defn -main [& args] | |
(println (filter #(> (count %1) 1) (anafind (get-words-from-file "some_file"))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment