Skip to content

Instantly share code, notes, and snippets.

@jkk
Created August 19, 2010 14:00
Show Gist options
  • Select an option

  • Save jkk/537945 to your computer and use it in GitHub Desktop.

Select an option

Save jkk/537945 to your computer and use it in GitHub Desktop.
;; http://codekata.pragprog.com/2007/01/kata_six_anagra.html
(ns user
(:use [clojure.string :only [join split-lines lower-case]]))
;; map of normalized (with sorted letters) word to set of anagrams
(def anagram-map
(let [words (split-lines (slurp "/usr/share/dict/words"))]
(reduce
(fn [m word]
(let [normword (join (sort (lower-case word)))]
(assoc m
normword (conj (m normword #{}) word))))
{}
words)))
(def anagrams
(filter #(< 1 (count %))
(vals anagram-map)))
;; http://groups.google.com/group/clojure/browse_thread/thread/ca715954a42655bd
(def top-10
(take 10 (sort-by (comp count val) > anagram-map)))
(doseq [[normword anagrams] top-10]
(println (count anagrams) normword ":" anagrams))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment