-
-
Save seeflanigan/6500072 to your computer and use it in GitHub Desktop.
Anagram Solutions
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
(ns anagram | |
(:require [clojure.string :refer [lower-case split]])) | |
(defn- duplicate? [a b] | |
(= (lower-case a) (lower-case b))) | |
(defn- unique-candidates [source candidates] | |
(remove #(duplicate? % source) candidates)) | |
(defn- canonicalize [word] | |
(frequencies (lower-case word))) | |
(defn- is_anagram? [canonical_source candidate] | |
(= canonical_source (canonicalize candidate))) | |
(defn anagrams-for [source, candidates] | |
(->> | |
(unique-candidates source candidates) | |
(filter #(is_anagram? (canonicalize source) %)))) |
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
defmodule Anagram do | |
defp duplicate?(a, b) do | |
String.downcase(a) == String.downcase(b) | |
end | |
defp unique_words(source, candidates) do | |
Enum.reject(candidates, duplicate?(&1, source)) | |
end | |
defp canonical(string) do | |
String.downcase(string) |> String.graphemes |> Enum.sort | |
end | |
defp is_anagram?(canonical_source, candidate) do | |
canonical_source == canonical(candidate) | |
end | |
def match(source, candidates) do | |
unique_words(source, candidates) | |
|> Enum.filter(is_anagram?(canonical(source), &1)) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment