Skip to content

Commit

Permalink
Merge pull request JuliaLang#91 from Teo-ShaoWei/ex-phone-number
Browse files Browse the repository at this point in the history
Add exercise: phone-number
  • Loading branch information
SaschaMann authored Oct 19, 2017
2 parents 5cc8f7b + 1c0dd31 commit f469c7d
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
36 changes: 36 additions & 0 deletions exercises/phone-number/README.md
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.
18 changes: 18 additions & 0 deletions exercises/phone-number/example.jl
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
3 changes: 3 additions & 0 deletions exercises/phone-number/phone-number.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
function clean(phone_number)

end
49 changes: 49 additions & 0 deletions exercises/phone-number/runtests.jl
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

0 comments on commit f469c7d

Please sign in to comment.