ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# DanCouper's solution

## to Anagram in the ReasonML Track

Published at Mar 15 2019 · 0 comments
Instructions
Test suite
Solution

#### Note:

This exercise has changed since this solution 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"`.

## Source

Inspired by the Extreme Startup game https://github.com/rchatley/extreme_startup

## Building and testing

You will need the node package manager (npm) installed - download from here There is one time setup for each exercise, which may take a few minutes:

``````npm install
``````

Open two shells, and in the first, start the build process.

``````npm start
``````

In the second, start the tests running.

``````npm test
``````

As you edit the code, the two processes will continually rebuild and rerun the tests.

### Anagram_test.re

``````open Jest;
open Expect;
open Anagram;

describe("Anagram", () => {
test("no matches", () =>
expect(anagrams("diaper", ["hello", "world", "zombies", "pants"])) |> toEqual([])
);
test("detects two anagrams", () =>
expect(anagrams("master", ["stream", "pigeon", "maters"])) |> toEqual(["stream", "maters"])
);
test("does not detect anagram subsets", () =>
expect(anagrams("good", ["dog", "goody"]))  |> toEqual([])
);
test("detects anagram", () =>
expect(anagrams("listen", ["enlists", "google", "inlets", "banana"]))  |> toEqual(["inlets"])
);
test("detects three anagrams", () =>
expect(anagrams("allergy", ["gallery", "ballerina", "regally", "clergy", "largely", "leading"]))  |> toEqual(["gallery", "regally", "largely"])
);
test("does not detect non-anagrams(with identical checksum", () =>
expect(anagrams("mass", ["last"]))  |> toEqual([])
);
test("detects anagrams(case-insensitively", () =>
expect(anagrams("Orchestra", ["cashregister", "Carthorse", "radishes"]))  |> toEqual(["Carthorse"])
);
test("detects anagrams(using case-insensitive subject", () =>
expect(anagrams("Orchestra", ["cashregister", "carthorse", "radishes"])) |> toEqual(["carthorse"])
);
test("detects anagrams(using case-insensitive possible matches", () =>
expect(anagrams("orchestra", ["cashregister", "Carthorse", "radishes"])) |> toEqual(["Carthorse"])
);
test("does not detect a anagram if the original word is repeated", () =>
expect(anagrams("go", ["go Go GO"])) |> toEqual([])
);
test("anagrams(must use all letters exactly once", () =>
expect(anagrams("tapper", ["patter"])) |> toEqual([])
);
test("capital word is not own anagram", () =>
expect(anagrams("BANANA", ["Banana"])) |> toEqual([])
);
})``````
``````let isAnagramOf = (word1, word2) => {
let (w1len, w2len) = (String.length(word1), String.length(word2));

/* If the words are different lengths, bail immediately, else iterate char values */
(w1len != w2len) ? false : {
let (w1, w2) = (String.lowercase(word1), String.lowercase(word2));

/* Identical words are not anagrams, so bail immediately... */
(w1 == w2) ? false : {
/* ...otherwise perform a counting sort, iterating in lockstep
* over the characters of the two words. */
let rec countingSort = (i, alphabetChars) => switch (i) {
/* If the index counter has gone past the last index of
* the words, check the alphabet array is all zeroes;
* if not then the letters of the words cannot be the same... */
| n when n == w1len =>  alphabetChars |> Array.to_list |> List.for_all((v) => v == 0)
/* ...otherwise, increment the current character code of w1 &
* decrement the current character code of w2 in the
* char count array, then move onto the next characters. */
| n => {
let c1index = int_of_char(w1.[n]) - 97;
let c2index = int_of_char(w2.[n]) - 97;
alphabetChars[c1index] = alphabetChars[c1index] + 1;
alphabetChars[c2index] = alphabetChars[c2index] - 1;
countingSort(i + 1, alphabetChars);
}
}
countingSort(0, Array.make(26, 0));
};
};
}

let anagrams = (word, possibilities) => List.filter((poss) => isAnagramOf(word, poss), possibilities);``````

### 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?