Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[New Exercise]: Phone Number #241

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 10 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -435,6 +435,16 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "phone-number",
"name": "Phone Number",
"uuid": "bc5112c7-3c1c-4f10-8d02-90dd100a2180",
"practices": [
"regular-expressions"
],
"prerequisites": [],
"difficulty": 3
},
{
"slug": "nth-prime",
"name": "Nth Prime",
Expand Down
32 changes: 32 additions & 0 deletions exercises/practice/phone-number/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# Instructions

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 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.
70 changes: 70 additions & 0 deletions exercises/practice/phone-number/.meta/PhoneNumber.example.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
Function Get-PhoneNumber() {
<#
.SYNOPSIS
Clean up user-entered phone numbers so that they can be sent SMS messages.

.DESCRIPTION
Given a phone number string, check if it's a valid phone number that complied with the NANP system.
Return the cleaned number string if it's valid, otherwise throw the relevant error.
Also provide user the option to print out the number in pretty format.

.PARAMETER Number
The phone number string to be processed.

.PARAMETER Pretty
glaxxie marked this conversation as resolved.
Show resolved Hide resolved
Provide optional flag that will print out the phone number in pretty format: (Area)-Exchange-Number

.EXAMPLE
Get-PhoneNumber -Number '+1 (223) 456-7890'
return: '2234567890'

Get-PhoneNumber -Number '555.888.9999' -Pretty
return: '(555)-888-9999'
#>
[CmdletBinding()]
Param(
[string]$Number,
[switch]$Pretty
)

(Get-Digits $Number) -match '^(\d?)(\d{3})(\d{3})(\d{4})$' | Out-Null
$CountryCode = $Matches[1]
$AreaCode = $Matches[2]
$ExchangeCode = $Matches[3]
$LineNumber = $Matches[4]

Test-Number $CountryCode $AreaCode $ExchangeCode
if ($Pretty) {
return "($AreaCode)-$ExchangeCode-$LineNumber"
}
return "$AreaCode$ExchangeCode$LineNumber"
}
Function Get-Digits($String) {
if ($String -match '[a-zA-Z]') {
Throw 'Letters not permitted'
}
if ($String -match '[^\s\w+()-.]') {
Throw 'Punctuations not permitted'
}

$Digits = $String -replace '[^\d]',''
if ($Digits.Length -lt 10) {
Throw "Number can't be fewer than 10 digits"
}
if ($Digits.Length -gt 11) {
Throw "Number can't be more than 11 digits"
}
return $Digits
}

Function Test-Number($Country, $Area, $Exchange) {
if ($Country -and $Country -ne '1') {
Throw '11 digits must start with 1'
}
if ($Area[0] -eq '1' -or $Area[0] -eq '0') {
Throw "Area code can't start with $($Area[0])"
}
if ($Exchange[0] -eq '1' -or $Exchange[0] -eq '0') {
Throw "Exchange code can't start with $($Exchange[0])"
}
}
17 changes: 17 additions & 0 deletions exercises/practice/phone-number/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["glaxxie"],
"files": {
"solution": [
"PhoneNumber.ps1"
],
"test": [
"PhoneNumber.tests.ps1"
],
"example": [
".meta/PhoneNumber.example.ps1"
]
},
"blurb": "Clean up user-entered phone numbers so that they can be sent SMS messages.",
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
"source_url": "https://turing.edu"
}
84 changes: 84 additions & 0 deletions exercises/practice/phone-number/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[79666dce-e0f1-46de-95a1-563802913c35]
description = "cleans the number"

[c360451f-549f-43e4-8aba-fdf6cb0bf83f]
description = "cleans numbers with dots"

[08f94c34-9a37-46a2-a123-2a8e9727395d]
description = "cleans numbers with multiple spaces"

[598d8432-0659-4019-a78b-1c6a73691d21]
description = "invalid when 9 digits"
include = false

[2de74156-f646-42b5-8638-0ef1d8b58bc2]
description = "invalid when 9 digits"
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"

[57061c72-07b5-431f-9766-d97da7c4399d]
description = "invalid when 11 digits does not start with a 1"

[9962cbf3-97bb-4118-ba9b-38ff49c64430]
description = "valid when 11 digits and starting with 1"

[fa724fbf-054c-4d91-95da-f65ab5b6dbca]
description = "valid when 11 digits and starting with 1 even with punctuation"

[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
description = "invalid when more than 11 digits"
include = false

[4a1509b7-8953-4eec-981b-c483358ff531]
description = "invalid when more than 11 digits"
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"

[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
description = "invalid with letters"
include = false

[eb8a1fc0-64e5-46d3-b0c6-33184208e28a]
description = "invalid with letters"
reimplements = "63f38f37-53f6-4a5f-bd86-e9b404f10a60"

[4bd97d90-52fd-45d3-b0db-06ab95b1244e]
description = "invalid with punctuations"
include = false

[065f6363-8394-4759-b080-e6c8c351dd1f]
description = "invalid with punctuations"
reimplements = "4bd97d90-52fd-45d3-b0db-06ab95b1244e"

[d77d07f8-873c-4b17-8978-5f66139bf7d7]
description = "invalid if area code starts with 0"

[c7485cfb-1e7b-4081-8e96-8cdb3b77f15e]
description = "invalid if area code starts with 1"

[4d622293-6976-413d-b8bf-dd8a94d4e2ac]
description = "invalid if exchange code starts with 0"

[4cef57b4-7d8e-43aa-8328-1e1b89001262]
description = "invalid if exchange code starts with 1"

[9925b09c-1a0d-4960-a197-5d163cbe308c]
description = "invalid if area code starts with 0 on valid 11-digit number"

[3f809d37-40f3-44b5-ad90-535838b1a816]
description = "invalid if area code starts with 1 on valid 11-digit number"

[e08e5532-d621-40d4-b0cc-96c159276b65]
description = "invalid if exchange code starts with 0 on valid 11-digit number"

[57b32f3d-696a-455c-8bf1-137b6d171cdf]
description = "invalid if exchange code starts with 1 on valid 11-digit number"
30 changes: 30 additions & 0 deletions exercises/practice/phone-number/PhoneNumber.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
Function Get-PhoneNumber() {
<#
.SYNOPSIS
Clean up user-entered phone numbers so that they can be sent SMS messages.

.DESCRIPTION
Given a phone number string, check if it's a valid phone number that complied with the NANP system.
Return the cleaned number string if it's valid, otherwise throw the relevant error.
Also provide user the option to print out the number in pretty format.

.PARAMETER Number
The phone number string to be processed.

.PARAMETER Pretty
Provide optional flag that will print out the phone number in pretty format: (Area)-Exchange-Number

.EXAMPLE
Get-PhoneNumber -Number '+1 (223) 456-7890'
return: '2234567890'

Get-PhoneNumber -Number '555.888.9999' -Pretty
return: '(555)-888-9999'
#>
[CmdletBinding()]
Param(
[string]$Number,
[switch]$Pretty
)
Throw "Please implement this function"
}
117 changes: 117 additions & 0 deletions exercises/practice/phone-number/PhoneNumber.tests.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
BeforeAll {
. ".\PhoneNumber.ps1"
}

Describe "Phone Number Test Cases" {
Context "Passing tests" {
It "cleans the number" {
$got = Get-PhoneNumber -Number '2234567890'
$want = '2234567890'

$got | Should -BeExactly $want
}

It "cleans numbers with dot" {
$got = Get-PhoneNumber -Number '223.456.7890'
$want = '2234567890'

$got | Should -BeExactly $want
}

It "cleans numbers with multiple spaces" {
$got = Get-PhoneNumber -Number ' 223 456 7890 '
$want = '2234567890'

$got | Should -BeExactly $want
}

It "cleans numbers with brackets and dash" {
$got = Get-PhoneNumber -Number '(223)(456)-7890'
$want = '2234567890'

$got | Should -BeExactly $want
}

It "valid when 11 digits and starting with 1" {
$got = Get-PhoneNumber -Number '12234567890'
$want = '2234567890'

$got | Should -BeExactly $want
}

It "valid when 11 digits and starting with 1 even with allowed punctuation" {
$got = Get-PhoneNumber -Number '+1. (223) 456-7890'
$want = '2234567890'

$got | Should -BeExactly $want
}

It "clean number prettified" {
$got = Get-PhoneNumber -Number '223-456-7890' -Pretty
$want = '(223)-456-7890'

$got | Should -BeExactly $want
}

It "valid 11 digits prettified" {
$got = Get-PhoneNumber -Number '+1 (223) 456.7890' -Pretty
$want = '(223)-456-7890'

$got | Should -BeExactly $want
}
}

Context "Invalid inputs" {
It "invalid when 9 digits" {
{ Get-PhoneNumber -Number '223456789' } | Should -Throw "Number can't be fewer than 10 digits"
}

It "invalid when 11 digits does not start with a 1" {
{ Get-PhoneNumber -Number '+02234567890' } | Should -Throw '11 digits must start with 1'
}

It "invalid when more than 11 digits" {
{ Get-PhoneNumber -Number '123456789011' } | Should -Throw "Number can't be more than 11 digits"
}

It "invalid with letters" {
{ Get-PhoneNumber -Number 'tel-456-7890' } | Should -Throw 'Letters not permitted'
}

It "invalid with punctuations" {
{ Get-PhoneNumber -Number '123-@:!-7890' } | Should -Throw 'Punctuations not permitted'
}

It "invalid if area code starts with 0" {
{ Get-PhoneNumber -Number '(023) 456-7890' } | Should -Throw "Area code can't start with 0"
}

It "invalid if area code starts with 1" {
{ Get-PhoneNumber -Number '(123) 456-7890' } | Should -Throw "Area code can't start with 1"
}

It "invalid if exchange code starts with 0" {
{ Get-PhoneNumber -Number '223-056-7890' } | Should -Throw "Exchange code can't start with 0"
}

It "invalid if exchange code starts with 1" {
{Get-PhoneNumber -Number '223-156-7890'} | Should -Throw "Exchange code can't start with 1"
}

It "invalid if area code starts with 0 on valid 11-digit number" {
{ Get-PhoneNumber -Number '+1 (023) 456-7890' } | Should -Throw "Area code can't start with 0"
}

It "invalid if area code starts with 1 on valid 11-digit number" {
{ Get-PhoneNumber -Number '+1 (123) 456-7890' } | Should -Throw "Area code can't start with 1"
}

It "invalid if exchange code starts with 0 on valid 11-digit number" {
{ Get-PhoneNumber -Number '+1 (223) 056-7890' } | Should -Throw "Exchange code can't start with 0"
}

It "invalid if exchange code starts with 1 on valid 11-digit number" {
{ Get-PhoneNumber -Number '+1 (223) 156-7890' } | Should -Throw "Exchange code can't start with 1"
}
}
}