From 1c0dd316b16aef8597e266a9acf86ba64ee2f5e2 Mon Sep 17 00:00:00 2001 From: ShaoWei Teo Date: Wed, 18 Oct 2017 23:59:48 +0800 Subject: [PATCH] Add exercise: phone-number --- config.json | 13 +++++++ exercises/phone-number/README.md | 36 +++++++++++++++++++ exercises/phone-number/example.jl | 18 ++++++++++ exercises/phone-number/phone-number.jl | 3 ++ exercises/phone-number/runtests.jl | 49 ++++++++++++++++++++++++++ 5 files changed, 119 insertions(+) create mode 100644 exercises/phone-number/README.md create mode 100644 exercises/phone-number/example.jl create mode 100644 exercises/phone-number/phone-number.jl create mode 100644 exercises/phone-number/runtests.jl diff --git a/config.json b/config.json index dc770b015d1b4..10ec3fd9db07d 100644 --- a/config.json +++ b/config.json @@ -283,6 +283,19 @@ "mathematics" ] }, + { + "uuid": "2e39f242-0dac-ca80-0a59-15c1850d6617a864d57", + "slug": "phone-number", + "core": false, + "unlocked_by": null, + "difficulty": 1, + "topics": [ + "pattern_matching", + "conditionals", + "strings", + "regular_expressions" + ] + }, { "uuid": "e5a6d640-cd76-4453-8247-fd34ffed4fe5", "slug": "transpose", diff --git a/exercises/phone-number/README.md b/exercises/phone-number/README.md new file mode 100644 index 0000000000000..71fb76d5734fa --- /dev/null +++ b/exercises/phone-number/README.md @@ -0,0 +1,36 @@ +# Phone Number + +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 + +```text +(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 formated 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. +## Source + +Event Manager by JumpstartLab [http://tutorials.jumpstartlab.com/projects/eventmanager.html](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. diff --git a/exercises/phone-number/example.jl b/exercises/phone-number/example.jl new file mode 100644 index 0000000000000..1e23a4874754d --- /dev/null +++ b/exercises/phone-number/example.jl @@ -0,0 +1,18 @@ +function clean(phone_number) + # Main machinery is in the regex expression. + NXX = "[2-9][\\d]{2}" + country = "(?:\\+?1)?" + area = "\\(?\\s*($NXX)\\s*\\)?" + exchange = "($NXX)" + subscriber = "([\\d]{4})" + fillers = "\\s*(?:\\.|-)?\\s*" + r = Regex("^\\s*$country$fillers$area$fillers$exchange$fillers$subscriber\\s*\$") + + result = match(r, phone_number) + + if result != nothing + result = string(result.captures...) + end + + return result +end diff --git a/exercises/phone-number/phone-number.jl b/exercises/phone-number/phone-number.jl new file mode 100644 index 0000000000000..25eaa70757ec6 --- /dev/null +++ b/exercises/phone-number/phone-number.jl @@ -0,0 +1,3 @@ +function clean(phone_number) + +end diff --git a/exercises/phone-number/runtests.jl b/exercises/phone-number/runtests.jl new file mode 100644 index 0000000000000..3f4dbd26ace50 --- /dev/null +++ b/exercises/phone-number/runtests.jl @@ -0,0 +1,49 @@ +using Base.Test + +include("phone-number.jl") + + +# Tests adapted from `problem-specifications//canonical-data.json` @ v1.2.0 +# Returns the cleaned phone number as a digit string if given number is valid, +# else returns `nothing`. + +const expected_number = "2234567890" +const valid_10digit_num = ( + "(223) 456-7890", + "223.456.7890", + "223 456 7890 ", +) +const valid_11digit_num = ( + "12234567890", + " 1 223 456 7890 ", + "+1 (223) 456-7890", +) +const invalid_num = ( + "1223456789", + "22234567890", + "321234567890", + "223-abc-7890", + "223-@:!-7890", + "(023) 456-7890", + "(123) 456-7890", + "(223) 056-7890", + "(223) 156-7890", +) + +@testset "clean 10-digit number" begin + @testset "$number" for number in valid_10digit_num + @test clean(number) == expected_number + end +end + +@testset "clean 11-digit number starting with 1" begin + @testset "$number" for number in valid_11digit_num + @test clean(number) == expected_number + end +end + +@testset "detect invalid number" begin + @testset "$number" for number in invalid_num + @test clean(number) == nothing + end +end