mirror of
https://github.com/rjNemo/exercism-elixir
synced 2026-06-06 02:16:48 +00:00
anagram
This commit is contained in:
parent
c43da91eb3
commit
bcc4a9f076
2 changed files with 9 additions and 16 deletions
|
|
@ -3,6 +3,13 @@ defmodule Anagram do
|
||||||
Returns all candidates that are anagrams of, but not equal to, 'base'.
|
Returns all candidates that are anagrams of, but not equal to, 'base'.
|
||||||
"""
|
"""
|
||||||
@spec match(String.t(), [String.t()]) :: [String.t()]
|
@spec match(String.t(), [String.t()]) :: [String.t()]
|
||||||
def match(base, candidates) do
|
def match(base, candidates),
|
||||||
end
|
do:
|
||||||
|
Enum.filter(
|
||||||
|
candidates,
|
||||||
|
&(String.downcase(&1) != String.downcase(base) and
|
||||||
|
to_graphemes(&1) == to_graphemes(base))
|
||||||
|
)
|
||||||
|
|
||||||
|
defp to_graphemes(string), do: String.downcase(string) |> String.graphemes() |> Enum.sort()
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -1,85 +1,71 @@
|
||||||
defmodule AnagramTest do
|
defmodule AnagramTest do
|
||||||
use ExUnit.Case
|
use ExUnit.Case
|
||||||
|
|
||||||
# @tag :pending
|
|
||||||
test "no matches" do
|
test "no matches" do
|
||||||
matches = Anagram.match("diaper", ~w(hello world zombies pants))
|
matches = Anagram.match("diaper", ~w(hello world zombies pants))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects two anagrams" do
|
test "detects two anagrams" do
|
||||||
matches = Anagram.match("solemn", ~w(lemons cherry melons))
|
matches = Anagram.match("solemn", ~w(lemons cherry melons))
|
||||||
assert matches == ~w(lemons melons)
|
assert matches == ~w(lemons melons)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "does not detect anagram subsets" do
|
test "does not detect anagram subsets" do
|
||||||
matches = Anagram.match("good", ~w(dog goody))
|
matches = Anagram.match("good", ~w(dog goody))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects anagram" do
|
test "detects anagram" do
|
||||||
matches = Anagram.match("listen", ~w(enlists google inlets banana))
|
matches = Anagram.match("listen", ~w(enlists google inlets banana))
|
||||||
assert matches == ~w(inlets)
|
assert matches == ~w(inlets)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects three anagrams" do
|
test "detects three anagrams" do
|
||||||
matches = Anagram.match("allergy", ~w(gallery ballerina regally clergy largely leading))
|
matches = Anagram.match("allergy", ~w(gallery ballerina regally clergy largely leading))
|
||||||
assert matches == ~w(gallery regally largely)
|
assert matches == ~w(gallery regally largely)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects multiple anagrams with different case" do
|
test "detects multiple anagrams with different case" do
|
||||||
matches = Anagram.match("nose", ~w(Eons ONES))
|
matches = Anagram.match("nose", ~w(Eons ONES))
|
||||||
assert matches == ~w(Eons ONES)
|
assert matches == ~w(Eons ONES)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "does not detect non-anagrams with identical checksum" do
|
test "does not detect non-anagrams with identical checksum" do
|
||||||
matches = Anagram.match("mass", ~w(last))
|
matches = Anagram.match("mass", ~w(last))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detect anagrams case-insensitively" do
|
test "detect anagrams case-insensitively" do
|
||||||
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
||||||
assert matches == ~w(Carthorse)
|
assert matches == ~w(Carthorse)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects anagrams using case-insensitive subject" do
|
test "detects anagrams using case-insensitive subject" do
|
||||||
matches = Anagram.match("Orchestra", ~w(cashregister carthorse radishes))
|
matches = Anagram.match("Orchestra", ~w(cashregister carthorse radishes))
|
||||||
assert matches == ~w(carthorse)
|
assert matches == ~w(carthorse)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "detects anagrams using case-insensitive possible matches" do
|
test "detects anagrams using case-insensitive possible matches" do
|
||||||
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
|
||||||
assert matches == ~w(Carthorse)
|
assert matches == ~w(Carthorse)
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "does not detect an anagram if the original word is repeated" do
|
test "does not detect an anagram if the original word is repeated" do
|
||||||
matches = Anagram.match("go", ~w(go Go GO))
|
matches = Anagram.match("go", ~w(go Go GO))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "anagrams must use all letters exactly once" do
|
test "anagrams must use all letters exactly once" do
|
||||||
matches = Anagram.match("tapper", ~w(patter))
|
matches = Anagram.match("tapper", ~w(patter))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "words are not anagrams of themselves (case-insensitive)" do
|
test "words are not anagrams of themselves (case-insensitive)" do
|
||||||
matches = Anagram.match("BANANA", ~w(BANANA Banana banana))
|
matches = Anagram.match("BANANA", ~w(BANANA Banana banana))
|
||||||
assert matches == []
|
assert matches == []
|
||||||
end
|
end
|
||||||
|
|
||||||
@tag :pending
|
|
||||||
test "words other than themselves can be anagrams" do
|
test "words other than themselves can be anagrams" do
|
||||||
matches = Anagram.match("LISTEN", ~w(Listen Silent LISTEN))
|
matches = Anagram.match("LISTEN", ~w(Listen Silent LISTEN))
|
||||||
assert matches == ~w(Silent)
|
assert matches == ~w(Silent)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue