🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉 # RumakBartosz's solution

## to Phone Number in the Erlang Track

Published at Jan 11 2021 · 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.

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

``````-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(Number) ->
Normalized = preprocess(Number),
case length(Normalized) =/= 10 of
true -> "0000000000";
false -> Normalized
end.

areacode(Number) ->
Normalized = preprocess(Number),
{Area, _Rest} = lists:split(3, Normalized),
Area.

pretty_print([\$1 | Rest]) when length(Rest) =:= 10 ->
pretty_print(Rest);
pretty_print(Number) ->
Area = areacode(Number),
{Number1, Number2} = lists:split(3, Number -- Area),
"(" ++ Area ++ ")" ++ " " ++ Number1 ++ "-" ++ Number2.

discard_non_digit([]) ->
[];
discard_non_digit([Head | Rest]) when Head < 48 orelse Head > 57 ->
discard_non_digit(Rest);
discard_non_digit([Head | Rest]) ->
[Head | discard_non_digit(Rest)].

discard_country_code(Number) ->
[Country | Rest] = string:split(Number, " "),
case lists:last(Country) =:= \$1 of
true -> Rest;
false -> Number
end.

preprocess([\$1 | Rest]) when length(Rest) =:= 10 ->
preprocess(Rest);
preprocess(Number) ->
Countryless = discard_country_code(Number),
discard_non_digit(Countryless).``````

## 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?
{"ft_lang_label":"__label__en","ft_lang_prob":0.73295593,"math_prob":0.7931692,"size":4712,"snap":"2021-21-2021-25","text_gpt3_token_len":1149,"char_repetition_ratio":0.12553102,"word_repetition_ratio":0.026446281,"special_character_ratio":0.2922326,"punctuation_ratio":0.15844156,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96261525,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-15T11:04:29Z\",\"WARC-Record-ID\":\"<urn:uuid:08b92b80-6956-4fb6-8b18-bc2a4bb8d062>\",\"Content-Length\":\"19794\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4122e5da-5c46-4f2b-bc40-3c111c12eb6b>\",\"WARC-Concurrent-To\":\"<urn:uuid:53ca39c9-4f66-49bd-beaa-6711effa647c>\",\"WARC-IP-Address\":\"52.19.49.24\",\"WARC-Target-URI\":\"https://exercism.io/tracks/erlang/exercises/phone-number/solutions/8fe40e9df32a442c9e98f51b4e8c504e\",\"WARC-Payload-Digest\":\"sha1:XJ52DWMEFASZLQK4JV2N73F76YAI7XEC\",\"WARC-Block-Digest\":\"sha1:7OD3QK5OSWXYTXPRWHOYQLV5ITJWNLUY\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487620971.25_warc_CC-MAIN-20210615084235-20210615114235-00589.warc.gz\"}"}