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

ikarachristos's solution

to Phone Number in the Erlang Track

Published at Feb 04 2021 · 0 comments
Test suite

Clean up user-entered phone numbers so that they can be sent SMS messages.

The North American Numbering Plan (NANP) is a telephone numbering system used by many countries in North America like the United States, Canada or Bermuda. All NANP-countries share the same international country code: 1.

NANP numbers are ten-digit numbers consisting of a three-digit Numbering Plan Area code, commonly known as area code, followed by a seven-digit local number. The first three digits of the local number represent the exchange code, followed by the unique four-digit number which is the subscriber number.

The format is usually represented as


where N is any digit from 2 through 9 and X is any digit from 0 through 9.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code (1) if present.

For example, the inputs

  • +1 (613)-995-0253
  • 613-995-0253
  • 1 613 995 0253
  • 613.995.0253

should all produce the output


Note: As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.

Running tests

In order to run the tests, issue the following command from the exercise directory:

For running the tests provided, rebar3 is used as it is the official build and dependency management tool for erlang now. Please refer to the tracks installation instructions on how to do that.

In order to run the tests, you can issue the following command from the exercise directory.

$ rebar3 eunit

Test versioning

Each problem defines a macro TEST_VERSION in the test file and verifies that the solution defines and exports a function test_version returning that same value.

To make tests pass, add the following to your solution:


test_version() ->

The benefit of this is that reviewers can see against which test version an iteration was written if, for example, a previously posted solution does not solve the current problem or passes current tests.


For detailed information about the Erlang track, please refer to the help page on the Exercism site. This covers the basic information on setting up the development environment expected by the exercises.


Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html

Submitting Incomplete Solutions

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




cleans_number_test() ->
  ?assertEqual("1234567890", phone_number:number("(123) 456-7890")).

cleans_number_with_dots_test() ->
  ?assertEqual("1234567890", phone_number:number("123.456.7890")).

valid_when_eleven_digits_test() ->
  ?assertEqual("1234567890", phone_number:number("11234567890")).

invalid_when_eleven_digits_test() ->
  ?assertEqual("0000000000", phone_number:number("21234567890")).

invalid_when_nine_digits_test() ->
  ?assertEqual("0000000000", phone_number:number("123456789")).

area_code_test() ->
  ?assertEqual("123", phone_number:areacode("1234567890")).

pretty_print_test() ->
  ?assertEqual("(123) 456-7890", phone_number:pretty_print("1234567890")),
  ?assertEqual("(123) 456-7890", phone_number:pretty_print("11234567890")).

-export([number/1, areacode/1, pretty_print/1]).

number(String) ->
  Number = lists:filter(fun(X) -> X >= $0 andalso X =< $9 end, String),
  case {Number, length(Number)} of
    {[$1|T], 11} -> T;
    {_, 10} -> Number;
    {_, _} -> "0000000000"

areacode(String) -> string:slice(number(String), 0, 3).

exchange_code(String) -> string:slice(number(String), 3, 3).

subscriber_number(String) -> string:slice(number(String), 6).

pretty_print(String) ->
  lists:flatten(io_lib:format("(~s) ~s-~s", [areacode(String), exchange_code(String), subscriber_number(String)])).

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?