Skip to content

Commit

Permalink
Add phone-number exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode committed Sep 7, 2024
1 parent 41f5495 commit 9785b25
Show file tree
Hide file tree
Showing 13 changed files with 512 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "phone-number",
"name": "Phone Number",
"uuid": "8a147f05-ebfe-4e64-8402-a642655591f3",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
Expand Down
9 changes: 9 additions & 0 deletions exercises/practice/phone-number/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Instruction append

If the input is invalid, return an empty string.

## Reserved Memory

Bytes 64-319 of the linear memory are reserved for the input string.

The input string can be modified in place if desired.
34 changes: 34 additions & 0 deletions exercises/practice/phone-number/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 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.

Sometimes they also have the country code (represented as `1` or `+1`) prefixed.

Your task is to clean up differently formatted telephone numbers by removing punctuation and the country code 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.
18 changes: 18 additions & 0 deletions exercises/practice/phone-number/.eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"root": true,
"extends": "@exercism/eslint-config-javascript",
"env": {
"jest": true
},
"overrides": [
{
"files": [
"*.spec.js"
],
"excludedFiles": [
"custom.spec.js"
],
"extends": "@exercism/eslint-config-javascript/maintainers"
}
]
}
28 changes: 28 additions & 0 deletions exercises/practice/phone-number/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"phone-number.wat"
],
"test": [
"phone-number.spec.js"
],
"example": [
".meta/proof.ci.wat"
],
"invalidator": [
"package.json"
]
},
"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",
"custom": {
"version.tests.compatibility": "jest-27",
"flag.tests.task-per-describe": false,
"flag.tests.may-run-long": false,
"flag.tests.includes-optional": false
}
}
110 changes: 110 additions & 0 deletions exercises/practice/phone-number/.meta/proof.ci.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
(module
(memory (export "mem") 1)

(global $ZERO i32 (i32.const 48))
(global $ONE i32 (i32.const 49))
(global $NINE i32 (i32.const 57))

(func (export "clean") (param $textOffset i32) (param $textLength i32) (result i32 i32)

(local $index i32)
(local $stop i32)
(local $dest i32)
(local $value i32)
(local $start i32)

(local.set $index (local.get $textOffset))
(local.set $stop
(i32.add
(local.get $textOffset)
(local.get $textLength)
)
)
(local.set $dest (local.get $textOffset))

(loop $process
(if (i32.lt_u (local.get $index) (local.get $stop)) (then
(local.set $value
(i32.load8_u (local.get $index))
)
(local.set $index
(i32.add
(local.get $index)
(i32.const 1)
)
)

(br_if
$process
(i32.lt_u (local.get $value) (global.get $ZERO))
)

(if (i32.gt_u (local.get $value) (global.get $NINE)) (then
(return (i32.const 0) (i32.const 0))
))

(i32.store8
(local.get $dest)
(local.get $value)
)
(local.set $dest
(i32.add
(local.get $dest)
(i32.const 1)
)
)

(br $process)
))
)

(local.set $start (local.get $textOffset))

(if (i32.eq
(local.get $dest)
(i32.add (local.get $start) (i32.const 11))
) (then
(if (i32.ne
(i32.load8_u (local.get $start))
(global.get $ONE)
) (then
(return (i32.const 0) (i32.const 0))
))
(local.set $start
(i32.add
(local.get $start)
(i32.const 1)
)
)
))

(if (i32.ne
(local.get $dest)
(i32.add (local.get $start) (i32.const 10))
) (then
(return (i32.const 0) (i32.const 0))
))

(if (i32.le_u
(i32.load8_u (local.get $start))
(global.get $ONE)
) (then
(return (i32.const 0) (i32.const 0))
))

(if (i32.le_u
(i32.load8_u (i32.add (local.get $start) (i32.const 3)))
(global.get $ONE)
) (then
(return (i32.const 0) (i32.const 0))
))

(return
(local.get $start)
(i32.sub
(local.get $dest)
(local.get $start)
)
)
)
)
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"
1 change: 1 addition & 0 deletions exercises/practice/phone-number/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
audit=false
21 changes: 21 additions & 0 deletions exercises/practice/phone-number/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2021 Exercism

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
4 changes: 4 additions & 0 deletions exercises/practice/phone-number/babel.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export default {
presets: ["@exercism/babel-preset-javascript"],
plugins: [],
};
34 changes: 34 additions & 0 deletions exercises/practice/phone-number/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"name": "@exercism/wasm-phone-number",
"description": "Exercism exercises in WebAssembly.",
"type": "module",
"private": true,
"license": "MIT",
"repository": {
"type": "git",
"url": "https://github.com/exercism/wasm",
"directory": "exercises/practice/phone-number"
},
"jest": {
"maxWorkers": 1
},
"devDependencies": {
"@babel/core": "^7.23.3",
"@exercism/babel-preset-javascript": "^0.4.0",
"@exercism/eslint-config-javascript": "^0.6.0",
"@types/jest": "^29.5.8",
"@types/node": "^20.9.1",
"babel-jest": "^29.7.0",
"core-js": "^3.33.2",
"eslint": "^8.54.0",
"jest": "^29.7.0"
},
"dependencies": {
"@exercism/wasm-lib": "^0.2.0"
},
"scripts": {
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
"lint": "eslint ."
}
}
Loading

0 comments on commit 9785b25

Please sign in to comment.