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).
Execute the tests with:
$ mix test
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
# @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
If you're stuck on something, it may help to look at some of the available resources out there where answers might be found.
Julien Vanier https://github.com/monkbroc
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
defmodule AcronymTest do use ExUnit.Case test "it produces acronyms from title case" do assert Acronym.abbreviate("Portable Networks Graphic") === "PNG" end @tag :pending test "it produces acronyms from lower case" do assert Acronym.abbreviate("Ruby on Rails") === "ROR" end @tag :pending test "it produces acronyms from inconsistent case" do assert Acronym.abbreviate("HyperText Markup Language") === "HTML" end @tag :pending test "it ignores punctuation" do assert Acronym.abbreviate("First in, First out") === "FIFO" end @tag :pending test "it produces acronyms ignoring punctuation and casing" do assert Acronym.abbreviate("Complementary Metal-Oxide semiconductor") === "CMOS" end end
ExUnit.start() ExUnit.configure(exclude: :pending, trace: true)
defmodule Acronym do @character_after_acronym_target ~r/ (?! # negative lookahead - after asserting that what follows the # current position is not: \b\w # 1. a border character then a letter | # or [A-Z] # 2. a capital letter ) # (ie an acronym target) . # match any single character /x @doc """ Generate an acronym from a string. "This is a string" => "TIAS" """ @spec abbreviate(String.t()) :: String.t() def abbreviate(string) do string |> String.replace(@character_after_acronym_target, "") |> String.upcase() end end
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.