🎉 Exercism Research is now launched. Help Exercism, help science and have some fun at research.exercism.io 🎉
Avatar of epequeno

epequeno's solution

to Phone Number in the Elm Track

Published at Apr 23 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.

Elm Installation

Refer to the Installing Elm page for information about installing elm.

Writing the Code

The code you have to write is located inside the src/ directory of the exercise. Elm automatically installs packages dependencies the first time you run the tests so we can start by running the tests from the exercise directory with:

$ elm-test

To automatically run tests again when you save changes:

$ elm-test --watch

As you work your way through the tests suite in the file tests/Tests.elm, be sure to remove the skip <| calls from each test until you get them all passing!

Source

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

Submitting Incomplete Solutions

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

Tests.elm

module Tests exposing (tests)

import Expect
import PhoneNumber exposing (getNumber)
import Test exposing (..)


tests : Test
tests =
    describe "PhoneNumber"
        [ test "cleans the number" <|
            \() -> Expect.equal (Just "2234567890") (getNumber "(223) 456-7890")
        , skip <|
            test "cleans numbers with dots" <|
                \() -> Expect.equal (Just "2234567890") (getNumber "223.456.7890")
        , skip <|
            test "cleans numbers with multiple spaces" <|
                \() -> Expect.equal (Just "2234567890") (getNumber "223 456   7890   ")
        , skip <|
            test "invalid when 9 digits" <|
                \() -> Expect.equal Nothing (getNumber "223456789")
        , skip <|
            test "invalid when 11 digits does not start with a 1" <|
                \() -> Expect.equal Nothing (getNumber "22234567890")
        , skip <|
            test "valid when 11 digits and starting with 1" <|
                \() -> Expect.equal (Just "2234567890") (getNumber "12234567890")
        , skip <|
            test "valid when 11 digits and starting with 1 even with punctuation" <|
                \() -> Expect.equal (Just "2234567890") (getNumber "+1 (223) 456-7890")
        , skip <|
            test "invalid when more than 11 digits" <|
                \() -> Expect.equal Nothing (getNumber "321234567890")
        , skip <|
            test "invalid with letters" <|
                \() -> Expect.equal Nothing (getNumber "123-abc-7890")
        , skip <|
            test "invalid with punctuations" <|
                \() -> Expect.equal Nothing (getNumber "123-@:!-7890")
        , skip <|
            test "invalid if area code starts with 0" <|
                \() -> Expect.equal Nothing (getNumber "(023) 456-7890")
        , skip <|
            test "invalid if area code starts with 1" <|
                \() -> Expect.equal Nothing (getNumber "(123) 456-7890")
        , skip <|
            test "invalid if exchange code starts with 0" <|
                \() -> Expect.equal Nothing (getNumber "(223) 056-7890")
        , skip <|
            test "invalid if exchange code starts with 1" <|
                \() -> Expect.equal Nothing (getNumber "(223) 156-7890")
        , skip <|
            test "invalid if area code starts with 0 on valid 11-digit number" <|
                \() -> Expect.equal Nothing (getNumber "1 (023) 456-7890")
        , skip <|
            test "invalid if area code starts with 1 on valid 11-digit number" <|
                \() -> Expect.equal Nothing (getNumber "1 (123) 456-7890")
        , skip <|
            test "invalid if exchange code starts with 0 on valid 11-digit number" <|
                \() -> Expect.equal Nothing (getNumber "1 (223) 056-7890")
        , skip <|
            test "invalid if exchange code starts with 1 on valid 11-digit number" <|
                \() -> Expect.equal Nothing (getNumber "1 (223) 156-7890")
        ]
module PhoneNumber exposing (getNumber)


getNumber : String -> Maybe String
getNumber phoneNumber =
    let
        number =
            phoneNumber |> String.filter Char.isDigit
    in
    case String.length number of
        11 ->
            if String.startsWith "1" number then
                check (String.dropLeft 1 number)

            else
                Nothing

        10 ->
            check number

        _ ->
            Nothing


check : String -> Maybe String
check number =
    let
        ( a, b ) =
            ( getIx 0 number, getIx 3 number )
    in
    if (a < 2) || (b < 2) then
        Nothing

    else
        Just number


getIx : Int -> String -> Int
getIx ix s =
    s
        |> String.slice ix (ix + 1)
        |> String.toInt
        |> Maybe.withDefault 0

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?