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

alex123098's solution

to Phone Number in the F# Track

Published at Nov 09 2018 · 3 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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.

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

Submitting Incomplete Solutions

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

PhoneNumberTest.fs

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

module PhoneNumberTest

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 to run test")>]
let ``Cleans numbers with dots`` () =
    let expected: Result<uint64,string> = Ok 2234567890UL
    clean "223.456.7890" |> should equal expected

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

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

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

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

[<Fact(Skip = "Remove to run 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 to run 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 to run 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 to run 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 to run 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 to run 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 to run 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 to run 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 remove subString (input: string) = input.Replace (subString, "")

let startsWith (s: string) (input: string) = input.StartsWith s

let removeLeadingPlus input = if input |> startsWith "+" then input.Substring 1 else input

let cleanUpInput =
    removeLeadingPlus >> remove "(" >> remove ")" >> remove " " >> remove "-" >> remove "."

let vNoLetters input = 
    if String.exists Char.IsLetter input 
    then Error "alphanumerics not permitted" else Ok input

let vNoPunctuation input =
    if String.exists Char.IsPunctuation input
    then Error "punctuations not permitted" else Ok input

let vCountryCode input =
    match String.length input with
    | 11 when input.[0] = '1' -> Ok (input.Substring 1) 
    | 11 -> Error "11 digits must start with 1"
    | _ -> Ok input

let vLength input = 
    match String.length input with
    | x when x > 11 -> Error "more than 11 digits"
    | x when x < 10 -> Error "incorrect number of digits"
    | _ -> Ok input

let vAreaCode (input: string) =
    match input.[0] with
    | '0' -> Error "area code cannot start with zero"
    | '1' -> Error "area code cannot start with one"
    | _ -> Ok input

let vExchange (input: string) =
    match input.[3] with
    | '0' -> Error "exchange code cannot start with zero"
    | '1' -> Error "exchange code cannot start with one"
    | _ -> Ok input


let clean input : Result<uint64, string> = 
    input
    |> cleanUpInput
    |> vLength
    |> Result.bind vCountryCode
    |> Result.bind vNoLetters
    |> Result.bind vNoPunctuation
    |> Result.bind vAreaCode
    |> Result.bind vExchange
    |> Result.map uint64

Community comments

Find this solution interesting? Ask the author a question to learn more.
Avatar of Palmer

Looks nice where can I learn more about Result.bind ?

Avatar of alex123098

There's an excellent topic written by Scott Wlaschin . It's not related to Result.bind function specifically, but to the concept of the bind function in general. Also, here's the book by the same author you may want to take a look at.

(edited over 1 year ago)
Avatar of Palmer

Thank you.

It's exactly what I needed.

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?