🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of ikarachristos

ikarachristos's solution

to Anagram in the Erlang Track

Published at Feb 01 2021 · 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

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Questions?

For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.

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_tests.erl

%% Generated with 'testgen v0.2.0'
%% Revision 1 of the exercises generator was used
%% https://github.com/exercism/problem-specifications/raw/42dd0cea20498fd544b152c4e2c0a419bb7e266a/exercises/anagram/canonical-data.json
%% This file is automatically generated from the exercises canonical data.

-module(anagram_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").




'1_no_matches_test_'() ->
    {"no matches",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("diaper",
						    ["hello", "world",
						     "zombies", "pants"])))}.

'2_detects_two_anagrams_test_'() ->
    {"detects two anagrams",
     ?_assertMatch(["maters", "stream"],
		   lists:sort(anagram:find_anagrams("master",
						    ["stream", "pigeon",
						     "maters"])))}.

'3_does_not_detect_anagram_subsets_test_'() ->
    {"does not detect anagram subsets",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("good",
						    ["dog", "goody"])))}.

'4_detects_anagram_test_'() ->
    {"detects anagram",
     ?_assertMatch(["inlets"],
		   lists:sort(anagram:find_anagrams("listen",
						    ["enlists", "google",
						     "inlets", "banana"])))}.

'5_detects_three_anagrams_test_'() ->
    {"detects three anagrams",
     ?_assertMatch(["gallery", "largely", "regally"],
		   lists:sort(anagram:find_anagrams("allergy",
						    ["gallery", "ballerina",
						     "regally", "clergy",
						     "largely", "leading"])))}.

'6_detects_multiple_anagrams_with_different_case_test_'() ->
    {"detects multiple anagrams with different "
     "case",
     ?_assertMatch(["Eons", "ONES"],
		   lists:sort(anagram:find_anagrams("nose",
						    ["Eons", "ONES"])))}.

'7_does_not_detect_non_anagrams_with_identical_checksum_test_'() ->
    {"does not detect non-anagrams with identical "
     "checksum",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("mass", ["last"])))}.

'8_detects_anagrams_case_insensitively_test_'() ->
    {"detects anagrams case-insensitively",
     ?_assertMatch(["Carthorse"],
		   lists:sort(anagram:find_anagrams("Orchestra",
						    ["cashregister",
						     "Carthorse",
						     "radishes"])))}.

'9_detects_anagrams_using_case_insensitive_subject_test_'() ->
    {"detects anagrams using case-insensitive "
     "subject",
     ?_assertMatch(["carthorse"],
		   lists:sort(anagram:find_anagrams("Orchestra",
						    ["cashregister",
						     "carthorse",
						     "radishes"])))}.

'10_detects_anagrams_using_case_insensitive_possible_matches_test_'() ->
    {"detects anagrams using case-insensitive "
     "possible matches",
     ?_assertMatch(["Carthorse"],
		   lists:sort(anagram:find_anagrams("orchestra",
						    ["cashregister",
						     "Carthorse",
						     "radishes"])))}.

'11_does_not_detect_an_anagram_if_the_original_word_is_repeated_test_'() ->
    {"does not detect an anagram if the original "
     "word is repeated",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("go", ["go Go GO"])))}.

'12_anagrams_must_use_all_letters_exactly_once_test_'() ->
    {"anagrams must use all letters exactly "
     "once",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("tapper",
						    ["patter"])))}.

'13_words_are_not_anagrams_of_themselves_case_insensitive_test_'() ->
    {"words are not anagrams of themselves "
     "(case-insensitive)",
     ?_assertMatch([],
		   lists:sort(anagram:find_anagrams("BANANA",
						    ["BANANA", "Banana",
						     "banana"])))}.

'14_words_other_than_themselves_can_be_anagrams_test_'() ->
    {"words other than themselves can be anagrams",
     ?_assertMatch(["Silent"],
		   lists:sort(anagram:find_anagrams("LISTEN",
						    ["Listen", "Silent",
						     "LISTEN"])))}.
-module(anagram).

-export([find_anagrams/2]).


find_anagrams(Subject, Candidates) ->
  LowercaseSubject = string:lowercase(Subject),
  lists:filtermap(fun(Candidate) -> is_anagram(LowercaseSubject, Candidate) end, Candidates).

is_anagram(LowercaseSubject, Candidate) ->
  LowercaseCandidate = string:lowercase(Candidate),
  LowercaseSubject =/= LowercaseCandidate andalso lists:sort(LowercaseSubject) =:= lists:sort(LowercaseCandidate).

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?