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.
To run the tests, run the command dotnet test
from within the exercise directory.
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.
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.
Event Manager by JumpstartLab http://tutorials.jumpstartlab.com/projects/eventmanager.html
// 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
type PhoneType = TenDigit | ElevenDigit
type ValidationFailure =
| CanNotContainLetters
| CanNotContainPunctuation
| LessThanTenDigits
| GreaterThanElevenDigits
| ElevenDigitsDoesntStartWithOne
| AreaCodeCanNotStartWithZero
| AreaCodeCanNotStartWithOne
| ExchangeCodeCanNotStartWithZero
| ExchangeCodeCanNotStartWithOne
let [<Literal>] zero = '0'
let [<Literal>] one = '1'
let getNumbers input = String.filter Char.IsNumber input
let getlength input = input |> getNumbers |> String.length
let getPhoneType input =
match getlength input with
| l when l = 10 -> TenDigit
| l when l = 11 -> ElevenDigit
| _ -> failwith "Invalid number of digits"
let removeCountryCode number =
match getPhoneType number with
| TenDigit -> number |> getNumbers
| ElevenDigit ->
number |> getNumbers |> Seq.tail |> String.Concat
let result rule failure input =
if rule
then Ok input
else Error failure
let validateAreaCode num reason input =
let firstNumber = input |> removeCountryCode |> Seq.tryHead
let rule = firstNumber <> Some num
input |> result rule reason
let validateExchange num reason input =
let firstNumber =
input
|> removeCountryCode
|> List.ofSeq
|> List.splitAt 3
|> snd
|> String.Concat
|> Seq.tryHead
let rule = firstNumber <> Some num
input |> result rule reason
let doesNotContainLetters input =
let rule = String.exists Char.IsLetter input |> not
input |> result rule CanNotContainLetters
let doesNotContainPunctuation input =
let allowedPunctuation = set "().-+"
let inputPunctuation =
input
|> String.filter(fun ch -> Char.IsPunctuation ch || ch ='+')
|> set
let rule = Set.union allowedPunctuation inputPunctuation = allowedPunctuation
input |> result rule CanNotContainPunctuation
let isLessTenDigits input =
let rule = getlength input > 9
input |> result rule LessThanTenDigits
let isGreaterElevenDigits input =
let rule = getlength input < 12
input |> result rule GreaterThanElevenDigits
let elevenDigitsNotStartsOne input =
match getPhoneType input with
| ElevenDigit ->
let firstNumber = input |> getNumbers |> Seq.tryHead
let rule = firstNumber = Some '1'
input |> result rule ElevenDigitsDoesntStartWithOne
| _ -> Ok input
let areaCodeNotStartsZero input =
input |> validateAreaCode zero AreaCodeCanNotStartWithZero
let areaCodeNotStartsOne input =
input |> validateAreaCode one AreaCodeCanNotStartWithOne
let exchangeNotStartsZero input =
input |> validateExchange zero ExchangeCodeCanNotStartWithZero
let exchangeNotStartsOne input =
input |> validateExchange one ExchangeCodeCanNotStartWithOne
let getErrorMessage = function
| CanNotContainLetters -> "letters not permitted"
| CanNotContainPunctuation -> "punctuations not permitted"
| LessThanTenDigits -> "incorrect number of digits"
| GreaterThanElevenDigits -> "more than 11 digits"
| ElevenDigitsDoesntStartWithOne -> "11 digits must start with 1"
| AreaCodeCanNotStartWithZero -> "area code cannot start with zero"
| AreaCodeCanNotStartWithOne -> "area code cannot start with one"
| ExchangeCodeCanNotStartWithZero -> "exchange code cannot start with zero"
| ExchangeCodeCanNotStartWithOne -> "exchange code cannot start with one"
let clean input =
let okWorkflow number = number |> removeCountryCode |> uint64
let errorWorkflow failure = getErrorMessage failure
Ok input
|> Result.bind doesNotContainLetters
|> Result.bind doesNotContainPunctuation
|> Result.bind isLessTenDigits
|> Result.bind isGreaterElevenDigits
|> Result.bind elevenDigitsNotStartsOne
|> Result.bind areaCodeNotStartsZero
|> Result.bind areaCodeNotStartsOne
|> Result.bind exchangeNotStartsZero
|> Result.bind exchangeNotStartsOne
|> Result.map okWorkflow
|> Result.mapError errorWorkflow
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.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments