Skip to content

Commit 8324420

Browse files
Add phone-number exercise (#46)
1 parent c9dc182 commit 8324420

File tree

8 files changed

+275
-0
lines changed

8 files changed

+275
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@
158158
"prerequisites": [],
159159
"difficulty": 3
160160
},
161+
{
162+
"slug": "phone-number",
163+
"name": "Phone Number",
164+
"uuid": "65e3f2a3-861a-4ce3-a1f3-0e30fed0d6a9",
165+
"practices": [],
166+
"prerequisites": [],
167+
"difficulty": 3
168+
},
161169
{
162170
"slug": "prime-factors",
163171
"name": "Prime Factors",
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Instructions
2+
3+
Clean up phone numbers so that they can be sent SMS messages.
4+
5+
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.
6+
All NANP-countries share the same international country code: `1`.
7+
8+
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.
9+
The first three digits of the local number represent the _exchange code_, followed by the unique four-digit number which is the _subscriber number_.
10+
11+
The format is usually represented as
12+
13+
```text
14+
NXX NXX-XXXX
15+
```
16+
17+
where `N` is any digit from 2 through 9 and `X` is any digit from 0 through 9.
18+
19+
Sometimes they also have the country code (represented as `1` or `+1`) prefixed.
20+
21+
Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code if present.
22+
23+
For example, the inputs
24+
25+
- `+1 (613)-995-0253`
26+
- `613-995-0253`
27+
- `1 613 995 0253`
28+
- `613.995.0253`
29+
30+
should all produce the output
31+
32+
`6139950253`
33+
34+
**Note:** As this exercise only deals with telephone numbers used in NANP-countries, only 1 is considered a valid country code.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# Introduction
2+
3+
You've joined LinkLine, a leading communications company working to ensure reliable connections for everyone.
4+
The team faces a big challenge: users submit phone numbers in all sorts of formats — dashes, spaces, dots, parentheses, and even prefixes.
5+
Some numbers are valid, while others are impossible to use.
6+
7+
Your mission is to turn this chaos into order.
8+
You'll clean up valid numbers, formatting them appropriately for use in the system.
9+
At the same time, you'll identify and filter out any invalid entries.
10+
11+
The success of LinkLine's operations depends on your ability to separate the useful from the unusable.
12+
Are you ready to take on the challenge and keep the connections running smoothly?
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"keiravillekode"
4+
],
5+
"files": {
6+
"solution": [
7+
"phone_number.fut"
8+
],
9+
"test": [
10+
"test.fut"
11+
],
12+
"example": [
13+
".meta/example.fut"
14+
]
15+
},
16+
"blurb": "Clean up user-entered phone numbers so that they can be sent SMS messages.",
17+
"source": "Exercise by the JumpstartLab team for students at The Turing School of Software and Design.",
18+
"source_url": "https://turing.edu"
19+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def normalize [n] (phrase: [n]u8): []u8 =
2+
let (i, a) = loop (i, a) = (0, replicate n '\0') for c in phrase do
3+
if c < '0' then (i, a) else
4+
assert (c <= '9') (i + 1, a with [i] = c)
5+
in
6+
a[0:i]
7+
8+
def check_country [n] (phrase: [n]u8): []u8 =
9+
if n == 11 && phrase[0] == '1' then phrase[1:] else
10+
assert (n == 10) phrase
11+
12+
def check_area [n] (phrase: [n]u8): []u8 =
13+
assert (phrase[0] >= '2') phrase
14+
15+
def check_exchange [n] (phrase: [n]u8): []u8 =
16+
assert (phrase[3] >= '2') phrase
17+
18+
def clean (phrase: []u8): []u8 =
19+
phrase
20+
|> normalize
21+
|> check_country
22+
|> check_area
23+
|> check_exchange
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[79666dce-e0f1-46de-95a1-563802913c35]
13+
description = "cleans the number"
14+
15+
[c360451f-549f-43e4-8aba-fdf6cb0bf83f]
16+
description = "cleans numbers with dots"
17+
18+
[08f94c34-9a37-46a2-a123-2a8e9727395d]
19+
description = "cleans numbers with multiple spaces"
20+
21+
[598d8432-0659-4019-a78b-1c6a73691d21]
22+
description = "invalid when 9 digits"
23+
include = false
24+
25+
[2de74156-f646-42b5-8638-0ef1d8b58bc2]
26+
description = "invalid when 9 digits"
27+
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"
28+
29+
[57061c72-07b5-431f-9766-d97da7c4399d]
30+
description = "invalid when 11 digits does not start with a 1"
31+
32+
[9962cbf3-97bb-4118-ba9b-38ff49c64430]
33+
description = "valid when 11 digits and starting with 1"
34+
35+
[fa724fbf-054c-4d91-95da-f65ab5b6dbca]
36+
description = "valid when 11 digits and starting with 1 even with punctuation"
37+
38+
[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
39+
description = "invalid when more than 11 digits"
40+
include = false
41+
42+
[4a1509b7-8953-4eec-981b-c483358ff531]
43+
description = "invalid when more than 11 digits"
44+
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"
45+
46+
[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
47+
description = "invalid with letters"
48+
include = false
49+
50+
[eb8a1fc0-64e5-46d3-b0c6-33184208e28a]
51+
description = "invalid with letters"
52+
reimplements = "63f38f37-53f6-4a5f-bd86-e9b404f10a60"
53+
54+
[4bd97d90-52fd-45d3-b0db-06ab95b1244e]
55+
description = "invalid with punctuations"
56+
include = false
57+
58+
[065f6363-8394-4759-b080-e6c8c351dd1f]
59+
description = "invalid with punctuations"
60+
reimplements = "4bd97d90-52fd-45d3-b0db-06ab95b1244e"
61+
62+
[d77d07f8-873c-4b17-8978-5f66139bf7d7]
63+
description = "invalid if area code starts with 0"
64+
65+
[c7485cfb-1e7b-4081-8e96-8cdb3b77f15e]
66+
description = "invalid if area code starts with 1"
67+
68+
[4d622293-6976-413d-b8bf-dd8a94d4e2ac]
69+
description = "invalid if exchange code starts with 0"
70+
71+
[4cef57b4-7d8e-43aa-8328-1e1b89001262]
72+
description = "invalid if exchange code starts with 1"
73+
74+
[9925b09c-1a0d-4960-a197-5d163cbe308c]
75+
description = "invalid if area code starts with 0 on valid 11-digit number"
76+
77+
[3f809d37-40f3-44b5-ad90-535838b1a816]
78+
description = "invalid if area code starts with 1 on valid 11-digit number"
79+
80+
[e08e5532-d621-40d4-b0cc-96c159276b65]
81+
description = "invalid if exchange code starts with 0 on valid 11-digit number"
82+
83+
[57b32f3d-696a-455c-8bf1-137b6d171cdf]
84+
description = "invalid if exchange code starts with 1 on valid 11-digit number"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
def clean (phrase: []u8): []u8 = ???
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
import "phone_number"
2+
3+
-- cleans the number
4+
-- ==
5+
-- input { "(223) 456-7890" }
6+
-- output { "2234567890" }
7+
8+
-- cleans numbers with dots
9+
-- ==
10+
-- input { "223.456.7890" }
11+
-- output { "2234567890" }
12+
13+
-- cleans numbers with multiple spaces
14+
-- ==
15+
-- input { "223 456 7890 " }
16+
-- output { "2234567890" }
17+
18+
-- invalid when 9 digits
19+
-- ==
20+
-- input { "123456789" }
21+
-- error: Error*
22+
23+
-- invalid when 11 digits does not start with a 1
24+
-- ==
25+
-- input { "22234567890" }
26+
-- error: Error*
27+
28+
-- valid when 11 digits and starting with 1
29+
-- ==
30+
-- input { "12234567890" }
31+
-- output { "2234567890" }
32+
33+
-- valid when 11 digits and starting with 1 even with punctuation
34+
-- ==
35+
-- input { "+1 (223) 456-7890" }
36+
-- output { "2234567890" }
37+
38+
-- invalid when more than 11 digits
39+
-- ==
40+
-- input { "321234567890" }
41+
-- error: Error*
42+
43+
-- invalid with letters
44+
-- ==
45+
-- input { "523-abc-7890" }
46+
-- error: Error*
47+
48+
-- invalid with punctuations
49+
-- ==
50+
-- input { "523-@:!-7890" }
51+
-- error: Error*
52+
53+
-- invalid if area code starts with 0
54+
-- ==
55+
-- input { "(023) 456-7890" }
56+
-- error: Error*
57+
58+
-- invalid if area code starts with 1
59+
-- ==
60+
-- input { "(123) 456-7890" }
61+
-- error: Error*
62+
63+
-- invalid if exchange code starts with 0
64+
-- ==
65+
-- input { "(223) 056-7890" }
66+
-- error: Error*
67+
68+
-- invalid if exchange code starts with 1
69+
-- ==
70+
-- input { "(223) 156-7890" }
71+
-- error: Error*
72+
73+
-- invalid if area code starts with 0 on valid 11-digit number
74+
-- ==
75+
-- input { "1 (023) 456-7890" }
76+
-- error: Error*
77+
78+
-- invalid if area code starts with 1 on valid 11-digit number
79+
-- ==
80+
-- input { "1 (123) 456-7890" }
81+
-- error: Error*
82+
83+
-- invalid if exchange code starts with 0 on valid 11-digit number
84+
-- ==
85+
-- input { "1 (223) 056-7890" }
86+
-- error: Error*
87+
88+
-- invalid if exchange code starts with 1 on valid 11-digit number
89+
-- ==
90+
-- input { "1 (223) 156-7890" }
91+
-- error: Error*
92+
93+
def main (phrase: []u8): []u8 =
94+
clean phrase

0 commit comments

Comments
 (0)