Avatar of davearonson

davearonson's solution

to Pangram 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 sentence is a pangram. A pangram (Greek: παν γράμμα, pan gramma, "every letter") is a sentence using every letter of the alphabet at least once. The best known English pangram is:

The quick brown fox jumps over the lazy dog.

The alphabet used consists of ASCII letters a to z, inclusive, and is case insensitive. Input will not contain non-ASCII symbols.

Running tests

Execute the tests with:

$ elixir pangram_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/Pangram

Submitting Incomplete Solutions

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

pangram_test.exs

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

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

defmodule PangramTest do
  use ExUnit.Case

  # @tag :pending
  test "empty sentence" do
    refute Pangram.pangram?("")
  end

  @tag :pending
  test "pangram with only lower case" do
    assert Pangram.pangram?("the quick brown fox jumps over the lazy dog")
  end

  @tag :pending
  test "missing character 'x'" do
    refute Pangram.pangram?("a quick movement of the enemy will jeopardize five gunboats")
  end

  @tag :pending
  test "another missing character 'x'" do
    refute Pangram.pangram?("the quick brown fish jumps over the lazy dog")
  end

  @tag :pending
  test "pangram with underscores" do
    assert Pangram.pangram?("the_quick_brown_fox_jumps_over_the_lazy_dog")
  end

  @tag :pending
  test "pangram with numbers" do
    assert Pangram.pangram?("the 1 quick brown fox jumps over the 2 lazy dogs")
  end

  @tag :pending
  test "missing letters replaced by numbers" do
    refute Pangram.pangram?("7h3 qu1ck brown fox jumps ov3r 7h3 lazy dog")
  end

  @tag :pending
  test "pangram with mixed case and punctuation" do
    assert Pangram.pangram?("Five quacking Zephyrs jolt my wax bed.")
  end

  @tag :pending
  test "pangram with non ascii characters" do
    assert Pangram.pangram?("Victor jagt zwölf Boxkämpfer quer über den großen Sylter Deich.")
  end

  @tag :pending
  test "pangram in alphabet other than ASCII" do
    refute Pangram.pangram?(
             "Широкая электрификация южных губерний даст мощный толчок подъёму сельского хозяйства."
           )
  end
end
defmodule Pangram do
  @doc """
  Determines if a word or sentence is a pangram.
  A pangram is a sentence using every letter of the alphabet at least once.

  Returns a boolean.

    ## Examples

      iex> Pangram.pangram?("the quick brown fox jumps over the lazy dog")
      true

  """

  @alphabet ?a..?z

  @spec pangram?(String.t) :: boolean
  def pangram?(sentence) do
    sentence
    |> String.downcase
    |> String.to_charlist
    |> Enum.filter(&(&1 >= ?a && &1 <= ?z))
    |> Enum.uniq
    |> Enum.count
    == (@alphabet |> Enum.count)
  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?