Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Anagram

exercises/anagram.livemd

Anagram

Mix.install([
  {:jason, "~> 1.4"},
  {:kino, "~> 0.9", override: true},
  {:youtube, github: "brooklinjazz/youtube"},
  {:hidden_cell, github: "brooklinjazz/hidden_cell"}
])

Navigation

Home Report An Issue PalindromeComprehension Product Search

Anagram

Two words that contain all the same letters are anagrams. For example bored and robed are anagrams.

  • Create a function anagram?/2 which determines if two strings are anagrams.
  • Create a function filter_anagrams/2 which filters a list based on if they are anagrams of a provided string.

Solution Example

defmodule Anagram do
  def anagram?(word, possible_anagram) do
    sort_string(word) == sort_string(possible_anagram)
  end

  def filter_anagrams(words, string) do
    Enum.filter(words, fn word -> anagram?(word, string) end)
  end

  defp sort_string(string) do
    String.split(string, "", trim: true) |> Enum.sort() |> Enum.join()
  end
end
defmodule Anagram do
  @moduledoc """
  Documentation for `Anagram`.
  """

  @doc """
  Determine if two strings are anagrams.

  ## Examples

    iex> Anagram.anagram?("abc", "bac")
    true

    iex> Anagram.anagram?("sit", "its")
    true

    iex> Anagram.anagram?("cats", "dogs")
    false

    iex> Anagram.anagram?("robed", "bored")
    true
  """
  def anagram?(word, possible_anagram) do
    sort(word) === sort(possible_anagram)
  end

  @doc """
  Filter anagrams.

  ## Examples

    iex> Anagram.filter_anagrams(["abc", "bca", "abcd", "cat"], "bac")
    ["abc", "bca"]

    iex> Anagram.filter_anagrams(["rams", "mars", "arms", "alarms"], "arms")
    ["rams", "mars", "arms"]

    iex> Anagram.filter_anagrams(["stop"], "go")
    []
  """
  def filter_anagrams(word_list, anagram) do
    Enum.filter(word_list, &anagram?(&1, anagram))
  end

  defp sort(string) do
    String.downcase(string)
    |> String.graphemes
    |> Enum.sort()
    |> Enum.join()
  end
end

Bonus: Anagram Solver

Given a string, return all possible permutations of the string to return a list of potential anagrams.

This is a HARD challenge. Move on to regular exercises before attempting this.

defmodule AnagramSolver do
  @doc """
  Return a list of all string permutations to solve for all anagrams.

  ## Examples

    iex> AnagramSolver.solve("a")
    ["a"]

    iex> AnagramSolver.solve("ab")
    ["ab", "ba"]

    iex> AnagramSolver.solve("abc")
    ["abc", "acb", "bac", "bca", "cab", "cba"]
  """
  def solve(string) do
    String.downcase(string)
    |> String.graphemes()
    |> Enum.sort()
    |> Enum.join("")
    |> IO.inspect()

    
  
    
  end
end

Commit Your Progress

DockYard Academy now recommends you use the latest Release rather than forking or cloning our repository.

Run git status to ensure there are no undesirable changes. Then run the following in your command line from the curriculum folder to commit your progress.

$ git add .
$ git commit -m "finish Anagram exercise"
$ git push

We’re proud to offer our open-source curriculum free of charge for anyone to learn from at their own pace.

We also offer a paid course where you can learn from an instructor alongside a cohort of your peers. We will accept applications for the June-August 2023 cohort soon.

Navigation

Home Report An Issue PalindromeComprehension Product Search