Last active
October 29, 2015 15:25
-
-
Save leiteg/7ce8a65e01b433c637bb to your computer and use it in GitHub Desktop.
Find anagrams for a given word in a dictionary.
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
; ---------------------------------------------------------- | |
; Find anagrams for a given word in a dictionary | |
; @authors Gustavo Leite, Bruno Souza | |
; @date October 28th 2015 | |
; | |
; Usage: (pretty-anagram "algo" dictionary) | |
; => Anagrams for algo: galo, gola, lago, olga | |
; | |
; To import it in core.clj, add the statement: | |
; (:require [lab.anagram :refer :all]) | |
; in the ns definition. | |
; ---------------------------------------------------------- | |
(ns lab.anagram | |
(:require [clojure.string :as str] | |
[lab.acentos :refer [remove-accents]])) | |
(def dictionary | |
(str/split-lines (slurp "./resources/substantivos.txt"))) | |
(defn prepare-word [word] | |
(apply str (-> word .toLowerCase remove-accents sort))) | |
(defn anagram [word dic] | |
(let [sorted (prepare-word word) | |
fdic (filter #(= (count word) (count %)) dic) | |
pdic (map prepare-word fdic) | |
pair (map vector fdic pdic)] | |
(->> (filter #(= sorted (get % 1)) pair) | |
(map #(get % 0))))) | |
(defn pretty-anagram [word dic] | |
(print (str "Anagrams for " word ": " (str/join ", " (anagram word dic)) "\n"))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment