Avatar of full-lisp

full-lisp's solution

to Anagram in the Clojure Track

Published at Jul 17 2019 · 0 comments
Test suite

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".


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.


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

(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"
                     "clergy"  "largely"   "leading"]]
           (anagram/anagrams-for "allergy" coll)))))

(deftest case-insensitive-anagrams
  (is (= ["Carthorse"]
         (let [coll ["cashregister" "Carthorse" "radishes"]]
           (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)

(defn word-to-chars [word]
  (seq word))

(defn count-in [value coll]
  (count (filter #(= value %) coll)))

(defn normalize-word [word]
  (clojure.string/lower-case word))

(defn mapify-word [word]
  (let [norm-word (normalize-word word)
        chars-set (set (word-to-chars norm-word))]
    (reduce #(assoc %1 %2 (count-in %2 norm-word)) {} chars-set)))

(defn filter-prospect-list [prospect-list base-word]
   (filter #(not= (normalize-word base-word) (normalize-word %)))))

(defn mapify-prospect-list [prospect-list base-word]
  (let [filtered-prospect-list (filter-prospect-list prospect-list base-word)]
     (map mapify-word)
     (zipmap filtered-prospect-list))))

(defn anagrams-for [word prospect-list] ;; <- arglist goes here
  (let [word-map (mapify-word word)
        prospect-list-maps (mapify-prospect-list prospect-list word)]
     (filter #(= word-map (second %)) prospect-list-maps)
     (map first))))

Community comments

Find this solution interesting? Ask the author a question to learn more.

What can you learn from this solution?

A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.

Here are some questions to help you reflect on this solution and learn the most from it.

  • What compromises have been made?
  • Are there new concepts here that you could read more about to improve your understanding?