Avatar of paulfioravanti

paulfioravanti's solution

to Anagram in the Elixir Track

Published at Jun 27 2019 · 0 comments
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".

Running tests

Execute the tests with:

$ mix test

Pending tests

In the test suites, all but the first test have been skipped.

Once you get a test passing, you can unskip the next one by commenting out the relevant @tag :pending with a # symbol.

For example:

# @tag :pending
test "shouting" do
  assert Bob.hey("WATCH OUT!") == "Whoa, chill out!"
end

Or, you can enable all the tests by commenting out the ExUnit.configure line in the test suite.

# ExUnit.configure exclude: :pending, trace: true

If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.

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

defmodule AnagramTest do
  use ExUnit.Case

  # @tag :pending
  test "no matches" do
    matches = Anagram.match("diaper", ~w(hello world zombies pants))
    assert matches == []
  end

  @tag :pending
  test "detects two anagrams" do
    matches = Anagram.match("master", ~w(stream pigeon maters))
    assert matches == ~w(stream maters)
  end

  @tag :pending
  test "does not detect anagram subsets" do
    matches = Anagram.match("good", ~w(dog goody))
    assert matches == []
  end

  @tag :pending
  test "detects anagram" do
    matches = Anagram.match("listen", ~w(enlists google inlets banana))
    assert matches == ~w(inlets)
  end

  @tag :pending
  test "detects three anagrams" do
    matches = Anagram.match("allergy", ~w(gallery ballerina regally clergy largely leading))
    assert matches == ~w(gallery regally largely)
  end

  @tag :pending
  test "detects multiple anagrams with different case" do
    matches = Anagram.match("nose", ~w(Eons ONES))
    assert matches == ~w(Eons ONES)
  end

  @tag :pending
  test "does not detect non-anagrams with identical checksum" do
    matches = Anagram.match("mass", ~w(last))
    assert matches == []
  end

  @tag :pending
  test "detect anagramss case-insensitively" do
    matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
    assert matches == ~w(Carthorse)
  end

  @tag :pending
  test "detects anagrams using case-insensitive subject" do
    matches = Anagram.match("Orchestra", ~w(cashregister carthorse radishes))
    assert matches == ~w(carthorse)
  end

  @tag :pending
  test "detects anagrams using case-insensitive possible matches" do
    matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
    assert matches == ~w(Carthorse)
  end

  @tag :pending
  test "does not detect an anagram if the original word is repeated" do
    matches = Anagram.match("go", ~w(go Go GO))
    assert matches == []
  end

  @tag :pending
  test "anagrams must use all letters exactly once" do
    matches = Anagram.match("tapper", ~w(patter))
    assert matches == []
  end

  @tag :pending
  test "words are not anagrams of themselves (case-insensitive)" do
    matches = Anagram.match("BANANA", ~w(BANANA Banana banana))
    assert matches == []
  end

  @tag :pending
  test "words other than themselves can be anagrams" do
    matches = Anagram.match("LISTEN", ~w(Listen Silent LISTEN))
    assert matches == ~w(Silent)
  end
end

test_helper.exs

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)
defmodule Anagram do
  @doc """
  Returns all candidates that are anagrams of, but not equal to, 'base'.
  """
  @spec match(String.t(), [String.t()]) :: [String.t()]
  def match(base, candidates) do
    word = String.downcase(base)
    letters = to_sorted_letters(word)
    Enum.filter(candidates, &anagram?(String.downcase(&1), word, letters))
  end

  defp to_sorted_letters(word) do
    word
    |> String.graphemes()
    |> Enum.sort()
  end

  defp anagram?(candidate, word, letters) do
    not same_word?(word, candidate) and same_letters?(letters, candidate)
  end

  defp same_word?(word, candidate), do: word == candidate

  defp same_letters?(letters, candidate) do
    letters == to_sorted_letters(candidate)
  end
end

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?