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

n0mn0m's solution

to Phone Number in the F# Track

Published at Nov 05 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 the tests

To run the tests, run the command dotnet test from within the exercise directory.

Autoformatting the code

F# source code can be formatted with the Fantomas tool.

After installing it with dotnet tool restore, run dotnet fantomas . to format code within the current directory.

Further information

For more detailed information about the F# track, including how to get help if you're having trouble, please visit the exercism.io F# language page.

Source

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

PhoneNumberTests.fs

// This file was auto-generated based on version 1.7.0 of the canonical data.

module PhoneNumberTests

open FsUnit.Xunit
open Xunit

open PhoneNumber

[<Fact>]
let ``Cleans the number`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "(223) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Cleans numbers with dots`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "223.456.7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Cleans numbers with multiple spaces`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "223 456   7890   " |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid when 9 digits`` () =
    let expected: Result<uint64,string> = Error "incorrect number of digits"
    clean "123456789" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid when 11 digits does not start with a 1`` () =
    let expected: Result<uint64,string> = Error "11 digits must start with 1"
    clean "22234567890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Valid when 11 digits and starting with 1`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "12234567890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Valid when 11 digits and starting with 1 even with punctuation`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "+1 (223) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid when more than 11 digits`` () =
    let expected: Result<uint64,string> = Error "more than 11 digits"
    clean "321234567890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid with letters`` () =
    let expected: Result<uint64,string> = Error "letters not permitted"
    clean "123-abc-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid with punctuations`` () =
    let expected: Result<uint64,string> = Error "punctuations not permitted"
    clean "123-@:!-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if area code starts with 0`` () =
    let expected: Result<uint64,string> = Error "area code cannot start with zero"
    clean "(023) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if area code starts with 1`` () =
    let expected: Result<uint64,string> = Error "area code cannot start with one"
    clean "(123) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if exchange code starts with 0`` () =
    let expected: Result<uint64,string> = Error "exchange code cannot start with zero"
    clean "(223) 056-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if exchange code starts with 1`` () =
    let expected: Result<uint64,string> = Error "exchange code cannot start with one"
    clean "(223) 156-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if area code starts with 0 on valid 11-digit number`` () =
    let expected: Result<uint64,string> = Error "area code cannot start with zero"
    clean "1 (023) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if area code starts with 1 on valid 11-digit number`` () =
    let expected: Result<uint64,string> = Error "area code cannot start with one"
    clean "1 (123) 456-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if exchange code starts with 0 on valid 11-digit number`` () =
    let expected: Result<uint64,string> = Error "exchange code cannot start with zero"
    clean "1 (223) 056-7890" |> should equal expected

[<Fact(Skip = "Remove this Skip property to run this test")>]
let ``Invalid if exchange code starts with 1 on valid 11-digit number`` () =
    let expected: Result<uint64,string> = Error "exchange code cannot start with one"
    clean "1 (223) 156-7890" |> should equal expected
module PhoneNumber

open System

let clean input =
    let hasPunctuation c = (Char.IsPunctuation c) && not ([ '.'; '('; ')'; '-' ] |> List.contains c)

    let checkChars s =
        match s with
        | s when String.exists Char.IsLetter s -> Error "letters not permitted"
        | s when String.exists hasPunctuation s -> Error "punctuations not permitted"
        | _ -> Ok s

    let checkLength (s: string) =
        match s.Length with
        | s when s > 11 -> Error "more than 11 digits"
        | s when s < 10 -> Error "incorrect number of digits"
        | _ -> Ok s

    let checkCountryCode (s: string) =
        match String.length s, s.[0] = '1' with
        | (11, false) -> Error "11 digits must start with 1"
        | (11, true) -> Ok s.[1..]
        | _ -> Ok s

    let checkDigitNotZeroOrOne n desc (s: string) =
        match s.[n] with
        | '0' -> Error(sprintf "%s cannot start with zero" desc)
        | '1' -> Error(sprintf "%s cannot start with one" desc)
        | _ -> Ok s

    let checkAreaCode = checkDigitNotZeroOrOne 0 "area code"

    let checkExchangeCode = checkDigitNotZeroOrOne 3 "exchange code"

    input
    |> checkChars
    |> Result.map (String.filter Char.IsNumber)
    |> Result.bind checkLength
    |> Result.bind checkCountryCode
    |> Result.bind checkAreaCode
    |> Result.bind checkExchangeCode
    |> Result.map UInt64.Parse

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?