Anagram
Mix.install([
{:kino, github: "livebook-dev/kino", override: true},
{:kino_lab, "~> 0.1.0-dev", github: "jonatanklosko/kino_lab"},
{:vega_lite, "~> 0.1.4"},
{:kino_vega_lite, "~> 0.1.1"},
{:benchee, "~> 0.1"},
{:ecto, "~> 3.7"},
{:math, "~> 0.7.0"},
{:faker, "~> 0.17.0"},
{:utils, path: "#{__DIR__}/../utils"},
{:tested_cell, git: "https://github.com/BrooklinJazz/tested_cell"}
])
Navigation
Anagram
Two words that contain all the same letters are anagrams. For example bored and robed are anagrams.
In the Elixir cell below:
Step 1
-
Create a module
Anagram
-
Create a function on the
Anagram
moduleis_anagram?/2
-
is_anagram?/2
should accept two lowercase strings and determine if they are anagrams.
Step 2
-
Create a function on the
Anagram
modulefilter_anagrams/2
. -
filter_anagrams/2
should accept a string as the first parameter, and a list of strings as the second parameter. -
filter_anagrams/2
should filter out values in the list of strings that are not anagrams of the first parameter string.
For example:
Anagram.is_anagram?("stop", "pots")
true
Anagram.filter_anagrams("acres", ["cares", "races", "scare", "tree", "bear"])
["cares", "races", "scare"]
ExUnit.start(auto_run: false)
defmodule Assertion do
use ExUnit.Case
test "" do
defmodule Anagram do
def is_anagram?(word, possible_anagram) do
end
def filter_anagrams(word, possible_anagrams) do
end
end
assert Anagram.is_anagram?("stop", "pots") == true
refute Anagram.is_anagram?("example", "nonanagram") == true
word = Utils.Factory.string()
generate_non_anagram = fn word ->
word <> Enum.random(["a", "b", "c"])
end
generate_anagram = fn word ->
String.split(word, "", trim: true) |> Enum.shuffle() |> Enum.join("")
end
assert Anagram.is_anagram?(word, generate_anagram.(word)),
"`is_anagram?/1` failed to identify anagram."
refute Anagram.is_anagram?(word, generate_non_anagram.(word)),
"`is_anagram?/1` failed to identify non-anagram."
non_anagrams = Enum.map(1..5, fn _ -> generate_non_anagram.(word) end)
anagrams = Enum.map(1..5, fn _ -> generate_anagram.(word) end)
result = Anagram.filter_anagrams(word, anagrams ++ non_anagrams)
assert is_list(result), "filter_anagrams/2 should return a list"
assert Enum.sort(result) == Enum.sort(anagrams), "filter_anagrams/2 failed to filter anagrams"
end
end
ExUnit.run()
# Make variables and modules defined in the test available.
# Also allows for exploration using the output of the cell.
defmodule Anagram do
def is_anagram?(word, possible_anagram) do
end
def filter_anagrams(word, possible_anagrams) do
end
end
Commit Your Progress
Run the following in your command line from the project folder to track and save your progress in a Git commit.
$ git add .
$ git commit -m "finish anagram exercise"