Skip to content

Instantly share code, notes, and snippets.

@maruks
Created June 18, 2012 22:06
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 maruks/2951012 to your computer and use it in GitHub Desktop.
Save maruks/2951012 to your computer and use it in GitHub Desktop.
my code from code dojo 8
(ns code-dojo.core
(:use [code-dojo.core] [clojure.math.combinatorics] [clojure.java.io :only (reader)])
(:use [speclj.core]))
(def dictionary
(map #(.toLowerCase %)
(line-seq (reader "english_words.txt"))))
(defn same-length [len coll]
(filter #(= len (count %)) coll))
(defn anagrams [string]
(let [perms (map #(apply str %) (permutations string))
strings (same-length (count string) dictionary)]
(filter #(some #{%} strings) perms)))
(describe "anagram test"
(it "tea has anagram eat"
(should (some #{"eat"} (anagrams "tea") )))
(it "tea doesnt have anagram tae"
(should-not (some #{"tae"} (anagrams "tea") )))
(it "zoo has anagram zoo"
(should (some #{"zoo"} (anagrams "zoo") )))
(it "all anagrams have the same lenght"
(should (every? #(= 3 (.length %)) (anagrams "zoo"))))
(it "headache doesnt have anagram ache"
(should-not (some #{"headache"} (anagrams "ache") ))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment