Exercism v3 launches on Sept 1st 2021. Learn more! ๐Ÿš€๐Ÿš€๐Ÿš€
Avatar of thekeele

thekeele's solution

to Acronym in the Elixir Track

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

Convert a phrase to its acronym.

Techies love their TLA (Three Letter Acronyms)!

Help generate some jargon by writing a program that converts a long name like Portable Network Graphics to its acronym (PNG).

Running tests

Execute the tests with:

$ elixir acronym_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.


Julien Vanier https://github.com/monkbroc

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("acronym.exs", __DIR__)

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

defmodule AcronymTest do
  use ExUnit.Case

  test "it produces acronyms from title case" do
    assert Acronym.abbreviate("Portable Networks Graphic") === "PNG"

  @tag :pending
  test "it produces acronyms from lower case" do
    assert Acronym.abbreviate("Ruby on Rails") === "ROR"

  @tag :pending
  test "it produces acronyms from inconsistent case" do
    assert Acronym.abbreviate("HyperText Markup Language") === "HTML"

  @tag :pending
  test "it ignores punctuation" do
    assert Acronym.abbreviate("First in, First out") === "FIFO"

  @tag :pending
  test "it produces acronyms ignoring punctuation and casing" do
    assert Acronym.abbreviate("Complementary Metal-Oxide semiconductor") === "CMOS"
defmodule Acronym do
  @doc """
  Generate an acronym from a string.
  "This is a string" => "TIAS"
  @spec abbreviate(String.t()) :: String.t()
  def abbreviate(string) do
    for word <- String.split(string, [" ", "-"]), into: "" do
      <<letter::binary-size(1), rest::binary>> = word

      String.upcase(letter) <> maybe_capital_letter(rest)

  defp maybe_capital_letter(rest) do
    Enum.reduce(String.codepoints(rest), "", fn point, acc ->
      <<value::8>> = point

      if value in ?A..?Z, do: point <> acc, else: acc

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?