# jacegu's solution

## to Anagram in the Clojure Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Given a word and a list of possible anagrams, select the correct sublist.

Given "listen" and a list of candidates like "enlists" "google" "inlets" "banana" the program should return a list containing "inlets".

## Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### anagram_test.clj

(ns anagram-test
(:require [clojure.test :refer [deftest is]]
anagram))

(deftest no-matches
(is (= []
(anagram/anagrams-for "diaper" ["hello" "world" "zombies" "pants"]))))

(deftest detect-simple-anagram
(is (= ["tan"] (anagram/anagrams-for "ant" ["tan" "stand" "at"]))))

(deftest does-not-confuse-different-duplicates
(is (= [] (anagram/anagrams-for "galea" ["eagle"]))))

(deftest eliminate-anagram-subsets
(is (= [] (anagram/anagrams-for "good" ["dog" "goody"]))))

(deftest detect-anagram
(is (= ["inlets"]
(let [coll ["enlists" "google" "inlets" "banana"]]
(anagram/anagrams-for "listen" coll)))))

(deftest multiple-anagrams
(is (= ["gallery" "regally" "largely"]
(let [coll ["gallery" "ballerina" "regally"
(anagram/anagrams-for "allergy" coll)))))

(deftest case-insensitive-anagrams
(is (= ["Carthorse"]
(anagram/anagrams-for "Orchestra" coll)))))

(deftest word-is-not-own-anagram
(is (= [] (anagram/anagrams-for "banana" ["banana"]))))

(deftest capital-word-is-not-own-anagram
(is (= [] (anagram/anagrams-for "BANANA" ["banana"]))))
(ns anagram (:use [clojure.string :only [lower-case]]))

(defn- characters [word]
(sort (lower-case word)))

(defn- same-characters-as? [word]
(let [characters-word (characters word)]
(fn [other]
(= characters-word (characters other)))))

(defn- anagram-of? [word]
(let [same-characters? (same-characters-as? word)]
(fn [candidate]
(same-characters? candidate))))

(defn- different? [word candidate]
(not= word candidate))

(defn anagrams-for [word candidates]
(let [anagram? (anagram-of? word)]
(filter #(and (different? word %) (anagram? %)) candidates)))

jacegu
Solution Author