forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request JuliaLang#91 from Teo-ShaoWei/ex-phone-number
Add exercise: phone-number
- Loading branch information
Showing
5 changed files
with
119 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
function clean(phone_number) | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |