Avatar of davearonson

davearonson's solution

to Isogram in the Elixir Track

Published at Jul 13 2018 · 0 comments
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.

Determine if a word or phrase is an isogram.

An isogram (also known as a "nonpattern word") is a word or phrase without a repeating letter, however spaces and hyphens are allowed to appear multiple times.

Examples of isograms:

  • lumberjacks
  • background
  • downstream
  • six-year-old

The word isograms, however, is not an isogram, because the s repeats.

Running tests

Execute the tests with:

$ elixir isogram_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

Wikipedia https://en.wikipedia.org/wiki/Isogram

Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

isogram_test.exs

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

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

defmodule IsogramTest do
  use ExUnit.Case

  test "isogram lowercase" do
    assert Isogram.isogram?("subdermatoglyphic")
  end

  @tag :pending
  test "not isogram lowercase " do
    refute Isogram.isogram?("eleven")
  end

  @tag :pending
  test "isogram uppercase" do
    assert Isogram.isogram?("DEMONSTRABLY")
  end

  @tag :pending
  test "not isogram uppercase" do
    refute Isogram.isogram?("ALPHABET")
  end

  @tag :pending
  test "isogram with dash" do
    assert Isogram.isogram?("hjelmqvist-gryb-zock-pfund-wax")
  end

  @tag :pending
  test "not isogram with dash" do
    refute Isogram.isogram?("twenty-five")
  end

  @tag :pending
  test "phrase is isogram" do
    assert Isogram.isogram?("emily jung schwartzkopf")
  end

  @tag :pending
  test "phrase is not isogram" do
    refute Isogram.isogram?("the quick brown fox")
  end
end
defmodule Isogram do
  @doc """
  Determines if a word or sentence is an isogram
  """
  @spec isogram?(String.t) :: boolean
  def isogram?(sentence) do
    ! repeats?(sentence |> String.graphemes
                        |> Enum.filter(&(&1 =~ ~r/^\pL$/u)),
               %{})
  end

  defp repeats?([],          _    ), do: false
  defp repeats?([char|rest], sofar)  do
    # TRIED to do this w/ a guard but couldn't figure out how
    if sofar[char] do
      true
    else
      repeats?(rest, Map.put(sofar, char, true))
    end
  end

end

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?