ðŸŽ‰ Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io ðŸŽ‰

# EmilienMottet's solution

## to Phone Number in the Erlang Track

Published at Sep 07 2020 · 0 comments
Instructions
Test suite
Solution

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

``````(NXX)-NXX-XXXX
``````

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

`6139950253`

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.

``````-export([test_version/0]).

test_version() ->
1.
``````

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.

## Questions?

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.

## Source

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.

### phone_number_tests.erl

``````-module(phone_number_tests).

-include_lib("erl_exercism/include/exercism.hrl").
-include_lib("eunit/include/eunit.hrl").

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")).``````
``````-module(phone_number).

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

number(String) ->
clean_number(String).

clean_number(String) ->
valid_number([ X || X <- String, X >= \$0, X =< \$9 ]).

valid_number(String = [ First | Tail]) when ( First == \$1 ) and (length(String) == 11) ->
Tail;
valid_number(String = [ First | _Tail]) when ( First /= \$1 ) or (length(String) /= 10) ->
"0000000000";
valid_number(String) ->
String.

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

pretty_print(String = [First | Tail]) when ( First == \$1 ) and (length(String) == 11) ->
pretty_print(Tail);
pretty_print(String) when length(String) == 10 ->
"(" ++ areacode(String) ++ ") " ++ string:slice(String,3,3) ++ "-" ++ string:slice(String,6,4).``````