Skip to content

Commit d0ff1b9

Browse files
feat: Add phone-number exercise (#180)
1 parent caddc3c commit d0ff1b9

File tree

7 files changed

+340
-0
lines changed

7 files changed

+340
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,14 @@
145145
"prerequisites": [],
146146
"difficulty": 3
147147
},
148+
{
149+
"slug": "phone-number",
150+
"name": "Phone Number",
151+
"uuid": "8af4128a-59c4-477f-9903-bec44f3e1555",
152+
"practices": [],
153+
"prerequisites": [],
154+
"difficulty": 3
155+
},
148156
{
149157
"uuid": "4af029c7-3154-4781-8c49-4269d84cadad",
150158
"slug": "triangle",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Instructions
2+
3+
Clean up user-entered 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.
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.v"
8+
],
9+
"test": [
10+
"run_test.v"
11+
],
12+
"example": [
13+
".meta/example.v"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
module main
2+
3+
import strings
4+
5+
fn clean(phrase string) !string {
6+
mut builder := strings.new_builder(phrase.len)
7+
for ch in phrase {
8+
if ch.is_letter() {
9+
return error('letters not permitted')
10+
}
11+
12+
if ch.is_digit() {
13+
builder << ch
14+
} else if !' ()+-.'.contains_u8(ch) {
15+
return error('punctuations not permitted')
16+
}
17+
}
18+
19+
num_digits := builder.len
20+
21+
if num_digits < 10 {
22+
return error('must not be fewer than 10 digits')
23+
}
24+
25+
if num_digits > 11 {
26+
return error('must not be greater than 11 digits')
27+
}
28+
29+
if num_digits == 11 && builder[0] != `1` {
30+
return error('11 digits must start with 1')
31+
}
32+
33+
mut result := builder.str()
34+
if num_digits == 11 {
35+
result = result.substr(1, 11)
36+
}
37+
38+
if result[0] == `0` {
39+
return error('area code cannot start with zero')
40+
}
41+
42+
if result[0] == `1` {
43+
return error('area code cannot start with one')
44+
}
45+
46+
if result[3] == `0` {
47+
return error('exchange code cannot start with zero')
48+
}
49+
50+
if result[3] == `1` {
51+
return error('exchange code cannot start with one')
52+
}
53+
54+
return result
55+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[79666dce-e0f1-46de-95a1-563802913c35]
6+
description = "cleans the number"
7+
8+
[c360451f-549f-43e4-8aba-fdf6cb0bf83f]
9+
description = "cleans numbers with dots"
10+
11+
[08f94c34-9a37-46a2-a123-2a8e9727395d]
12+
description = "cleans numbers with multiple spaces"
13+
14+
[598d8432-0659-4019-a78b-1c6a73691d21]
15+
description = "invalid when 9 digits"
16+
include = false
17+
18+
[2de74156-f646-42b5-8638-0ef1d8b58bc2]
19+
description = "invalid when 9 digits"
20+
reimplements = "598d8432-0659-4019-a78b-1c6a73691d21"
21+
22+
[57061c72-07b5-431f-9766-d97da7c4399d]
23+
description = "invalid when 11 digits does not start with a 1"
24+
25+
[9962cbf3-97bb-4118-ba9b-38ff49c64430]
26+
description = "valid when 11 digits and starting with 1"
27+
28+
[fa724fbf-054c-4d91-95da-f65ab5b6dbca]
29+
description = "valid when 11 digits and starting with 1 even with punctuation"
30+
31+
[c6a5f007-895a-4fc5-90bc-a7e70f9b5cad]
32+
description = "invalid when more than 11 digits"
33+
include = false
34+
35+
[4a1509b7-8953-4eec-981b-c483358ff531]
36+
description = "invalid when more than 11 digits"
37+
reimplements = "c6a5f007-895a-4fc5-90bc-a7e70f9b5cad"
38+
39+
[63f38f37-53f6-4a5f-bd86-e9b404f10a60]
40+
description = "invalid with letters"
41+
include = false
42+
43+
[eb8a1fc0-64e5-46d3-b0c6-33184208e28a]
44+
description = "invalid with letters"
45+
reimplements = "63f38f37-53f6-4a5f-bd86-e9b404f10a60"
46+
47+
[4bd97d90-52fd-45d3-b0db-06ab95b1244e]
48+
description = "invalid with punctuations"
49+
include = false
50+
51+
[065f6363-8394-4759-b080-e6c8c351dd1f]
52+
description = "invalid with punctuations"
53+
reimplements = "4bd97d90-52fd-45d3-b0db-06ab95b1244e"
54+
55+
[d77d07f8-873c-4b17-8978-5f66139bf7d7]
56+
description = "invalid if area code starts with 0"
57+
58+
[c7485cfb-1e7b-4081-8e96-8cdb3b77f15e]
59+
description = "invalid if area code starts with 1"
60+
61+
[4d622293-6976-413d-b8bf-dd8a94d4e2ac]
62+
description = "invalid if exchange code starts with 0"
63+
64+
[4cef57b4-7d8e-43aa-8328-1e1b89001262]
65+
description = "invalid if exchange code starts with 1"
66+
67+
[9925b09c-1a0d-4960-a197-5d163cbe308c]
68+
description = "invalid if area code starts with 0 on valid 11-digit number"
69+
70+
[3f809d37-40f3-44b5-ad90-535838b1a816]
71+
description = "invalid if area code starts with 1 on valid 11-digit number"
72+
73+
[e08e5532-d621-40d4-b0cc-96c159276b65]
74+
description = "invalid if exchange code starts with 0 on valid 11-digit number"
75+
76+
[57b32f3d-696a-455c-8bf1-137b6d171cdf]
77+
description = "invalid if exchange code starts with 1 on valid 11-digit number"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module main
2+
3+
fn clean(phrase string) !string {
4+
}
+143
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
module main
2+
3+
fn test_cleans_the_number() {
4+
phrase := '(223) 456-7890'
5+
assert clean(phrase)! == '2234567890'
6+
}
7+
8+
fn test_cleans_numbers_with_dots() {
9+
phrase := '223.456.7890'
10+
assert clean(phrase)! == '2234567890'
11+
}
12+
13+
fn test_cleans_numbers_with_multiple_spaces() {
14+
phrase := '223 456 7890 '
15+
assert clean(phrase)! == '2234567890'
16+
}
17+
18+
fn test_invalid_when_9_digits() {
19+
phrase := '123456789'
20+
if res := clean(phrase) {
21+
assert false, 'invalid when 9 digits should return an error'
22+
} else {
23+
assert err.msg() == 'must not be fewer than 10 digits'
24+
}
25+
}
26+
27+
fn test_invalid_when_11_digits_does_not_start_with_a_1() {
28+
phrase := '22234567890'
29+
if res := clean(phrase) {
30+
assert false, 'invalid when 11 digits does not start with a 1 should return an error'
31+
} else {
32+
assert err.msg() == '11 digits must start with 1'
33+
}
34+
}
35+
36+
fn test_valid_when_11_digits_and_starting_with_1() {
37+
phrase := '12234567890'
38+
assert clean(phrase)! == '2234567890'
39+
}
40+
41+
fn test_valid_when_11_digits_and_starting_with_1_even_with_punctuation() {
42+
phrase := '+1 (223) 456-7890'
43+
assert clean(phrase)! == '2234567890'
44+
}
45+
46+
fn test_invalid_when_more_than_11_digits() {
47+
phrase := '321234567890'
48+
if res := clean(phrase) {
49+
assert false, 'invalid when more than 11 digits should return an error'
50+
} else {
51+
assert err.msg() == 'must not be greater than 11 digits'
52+
}
53+
}
54+
55+
fn test_invalid_with_letters() {
56+
phrase := '523-abc-7890'
57+
if res := clean(phrase) {
58+
assert false, 'invalid with letters should return an error'
59+
} else {
60+
assert err.msg() == 'letters not permitted'
61+
}
62+
}
63+
64+
fn test_invalid_with_punctuations() {
65+
phrase := '523-@:!-7890'
66+
if res := clean(phrase) {
67+
assert false, 'invalid with punctuations should return an error'
68+
} else {
69+
assert err.msg() == 'punctuations not permitted'
70+
}
71+
}
72+
73+
fn test_invalid_if_area_code_starts_with_0() {
74+
phrase := '(023) 456-7890'
75+
if res := clean(phrase) {
76+
assert false, 'invalid if area code starts with 0 should return an error'
77+
} else {
78+
assert err.msg() == 'area code cannot start with zero'
79+
}
80+
}
81+
82+
fn test_invalid_if_area_code_starts_with_1() {
83+
phrase := '(123) 456-7890'
84+
if res := clean(phrase) {
85+
assert false, 'invalid if area code starts with 1 should return an error'
86+
} else {
87+
assert err.msg() == 'area code cannot start with one'
88+
}
89+
}
90+
91+
fn test_invalid_if_exchange_code_starts_with_0() {
92+
phrase := '(223) 056-7890'
93+
if res := clean(phrase) {
94+
assert false, 'invalid if exchange code starts with 0 should return an error'
95+
} else {
96+
assert err.msg() == 'exchange code cannot start with zero'
97+
}
98+
}
99+
100+
fn test_invalid_if_exchange_code_starts_with_1() {
101+
phrase := '(223) 156-7890'
102+
if res := clean(phrase) {
103+
assert false, 'invalid if exchange code starts with 1 should return an error'
104+
} else {
105+
assert err.msg() == 'exchange code cannot start with one'
106+
}
107+
}
108+
109+
fn test_invalid_if_area_code_starts_with_0_on_valid_11_digit_number() {
110+
phrase := '1 (023) 456-7890'
111+
if res := clean(phrase) {
112+
assert false, 'invalid if area code starts with 0 on valid 11-digit number should return an error'
113+
} else {
114+
assert err.msg() == 'area code cannot start with zero'
115+
}
116+
}
117+
118+
fn test_invalid_if_area_code_starts_with_1_on_valid_11_digit_number() {
119+
phrase := '1 (123) 456-7890'
120+
if res := clean(phrase) {
121+
assert false, 'invalid if area code starts with 1 on valid 11-digit number should return an error'
122+
} else {
123+
assert err.msg() == 'area code cannot start with one'
124+
}
125+
}
126+
127+
fn test_invalid_if_exchange_code_starts_with_0_on_valid_11_digit_number() {
128+
phrase := '1 (223) 056-7890'
129+
if res := clean(phrase) {
130+
assert false, 'invalid if exchange code starts with 0 on valid 11-digit number should return an error'
131+
} else {
132+
assert err.msg() == 'exchange code cannot start with zero'
133+
}
134+
}
135+
136+
fn test_invalid_if_exchange_code_starts_with_1_on_valid_11_digit_number() {
137+
phrase := '1 (223) 156-7890'
138+
if res := clean(phrase) {
139+
assert false, 'invalid if exchange code starts with 1 on valid 11-digit number should return an error'
140+
} else {
141+
assert err.msg() == 'exchange code cannot start with one'
142+
}
143+
}

0 commit comments

Comments
 (0)