Avatar of shrivatsas

shrivatsas's solution

to Anagram in the Clojure Track

Published at Sep 09 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".



Submitting Incomplete Solutions



(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
  (:use [clojure.string :only [lower-case]]))

(defn anagram? [word potential-anagram] 
    (not= (lower-case word) (lower-case potential-anagram))
    (= (sort (lower-case word)) (sort (lower-case potential-anagram)))))

(defn anagrams-for [word, potential-anagrams]
  (filter (partial anagram? word) potential-anagrams))

Community comments


What can you learn from this solution?



  • What compromises have been made?