Avatar of davearonson

davearonson's solution

to Word Count in the Elixir Track

Published at Jul 13 2018 · 1 comment
Test suite


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.

Given a phrase, count the occurrences of each word in that phrase.

For example for the input "olly olly in come free"

olly: 2
in: 1
come: 1
free: 1

Running tests

Execute the tests with:

$ elixir word_count_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!"

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.


This is a classic toy problem, but we were reminded of it by seeing it in the Go Tour.

Submitting Incomplete Solutions

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


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

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

defmodule WordsTest do
  use ExUnit.Case

  test "count one word" do
    assert Words.count("word") == %{"word" => 1}

  @tag :pending
  test "count one of each" do
    expected = %{"one" => 1, "of" => 1, "each" => 1}
    assert Words.count("one of each") == expected

  @tag :pending
  test "count multiple occurrences" do
    expected = %{"one" => 1, "fish" => 4, "two" => 1, "red" => 1, "blue" => 1}
    assert Words.count("one fish two fish red fish blue fish") == expected

  @tag :pending
  test "ignore punctuation" do
    expected = %{"car" => 1, "carpet" => 1, "as" => 1, "java" => 1, "javascript" => 1}
    assert Words.count("car : carpet as java : javascript!!&@$%^&") == expected

  @tag :pending
  test "include numbers" do
    expected = %{"testing" => 2, "1" => 1, "2" => 1}
    assert Words.count("testing, 1, 2 testing") == expected

  @tag :pending
  test "hyphens" do
    expected = %{"co-operative" => 1}
    assert Words.count("co-operative") == expected

  @tag :pending
  test "ignore underscores" do
    expected = %{"two" => 1, "words" => 1}
    assert Words.count("two_words") == expected

  @tag :pending
  test "normalize case" do
    expected = %{"go" => 3}
    assert Words.count("go Go GO") == expected

  @tag :pending
  test "German" do
    expected = %{"götterfunken" => 1, "schöner" => 1, "freude" => 1}
    assert Words.count("Freude schöner Götterfunken") == expected
defmodule Words do
  @doc """
  Count the number of words in the sentence.

  Words are compared case-insensitively.

  @spec count(String.t) :: map()
  def count(sentence) do
    do_count(%{}, List.flatten(Regex.scan(~r/[[:alpha:][:digit:]\-]+/iu,

  defp do_count(acc, []), do: acc

  defp do_count(acc, [""|tail]), do: acc |> do_count(tail)

  defp do_count(acc, [head|tail]) do
    acc |> update_word(head) |> do_count(tail)

  defp update_word(acc, word) do
    Map.put(acc, word, Map.get(acc, word, 0) + 1)


Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of davearonson

Briefer, and also played a bit with the spacing to separate and group things. Not sure what should be canonical order of versions of a function (as with do_count), other than "does it work"; I'm going for simplest first.

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?