Avatar of davearonson

davearonson's solution

to Anagram in the Elixir Track

Published at Jul 13 2018 · 1 comment
Instructions
Test suite
Solution

Note:

This solution was written on an old version of Exercism. The tests below might not correspond to the solution code, and the exercise may have changed since this code was written.

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:

$ elixir anagram_test.exs

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

For more detailed information about the Elixir track, please see the help page.

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

if !System.get_env("EXERCISM_TEST_EXAMPLES") do
  Code.load_file("anagram.exs", __DIR__)
end

ExUnit.start()
ExUnit.configure(exclude: :pending, trace: true)

defmodule AnagramTest do
  use ExUnit.Case

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

  @tag :pending
  test "detect simple anagram" do
    matches = Anagram.match("ant", ["tan", "stand", "at"])
    assert matches == ["tan"]
  end

  @tag :pending
  test "detect multiple anagrams" do
    matches = Anagram.match("master", ["stream", "pigeon", "maters"])
    assert matches == ["stream", "maters"]
  end

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

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

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

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

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

  @tag :pending
  test "detect anagrams with case-insensitive candidate" do
    matches = Anagram.match("orchestra", ~w(cashregister Carthorse radishes))
    assert matches == ["Carthorse"]
  end

  @tag :pending
  test "anagrams must not be the source word" do
    matches = Anagram.match("corn", ["corn", "dark", "Corn", "rank", "CORN", "cron", "park"])
    assert matches == ["cron"]
  end

  @tag :pending
  test "do not detect words based on checksum" do
    matches = Anagram.match("mass", ["last"])
    assert matches == []
  end
end
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
    candidates |> Enum.filter(&is_anagram?(String.downcase(&1),
                                           String.downcase(base)))
  end

  defp is_anagram?(base, base), do: false

  defp is_anagram?(candidate, base) do
    sort_letters(candidate) == sort_letters(base)
  end

  defp sort_letters(str), do: str |> String.split("") |> Enum.sort

end

Community comments

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

Okay, much simpler now, and even though it's downcasing the base for every candidate, and sorting it for every non-matching candidate, it's still reasonably fast. The prior solution executed on my 2012 Mac Mini at usually 0.09 seconds or occasionally 0.1, and this way is the other way around, close enough. (Though some of that may be from no longer bothering to rejoin the letters into a string.)

(Could alternately have done is_anagram? as candidate != base && sort_letters(candidate) == sort_letters(base), but wanted to still play with pattern-matching....)

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?