Skip to content

Commit 894d7ba

Browse files
authored
Add rotational-cipher exercise (#73)
* Add rotation-cipher exercise * Tidy up rotational-cipher - Mark second to last tests xtest - Remove unnecessary exp import - Use global constants for "A", "Z", "a" and "z" - Add instructions.append.md
1 parent 4011ca4 commit 894d7ba

File tree

13 files changed

+392
-0
lines changed

13 files changed

+392
-0
lines changed

config.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,13 @@
188188
"practices": [],
189189
"prerequisites": [],
190190
"difficulty": 2
191+
}, {
192+
"slug": "rotational-cipher",
193+
"name": "Rotational Cipher",
194+
"uuid" : "039a7a86-d954-4bb6-a144-93b2eb17b016",
195+
"practices" : [],
196+
"prerequisites": [],
197+
"difficulty": 4
191198
}
192199
]
193200
},
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
## Reserved Memory
2+
3+
Bytes 64-319 of the linear memory are reserved for the input string.
4+
5+
The input string can be modified in place if desired.
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Instructions
2+
3+
Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.
4+
5+
The Caesar cipher is a simple shift cipher that relies on transposing all the letters in the alphabet using an integer key between `0` and `26`.
6+
Using a key of `0` or `26` will always yield the same output due to modular arithmetic.
7+
The letter is shifted for as many values as the value of the key.
8+
9+
The general notation for rotational ciphers is `ROT + <key>`.
10+
The most commonly used rotational cipher is `ROT13`.
11+
12+
A `ROT13` on the Latin alphabet would be as follows:
13+
14+
```text
15+
Plain: abcdefghijklmnopqrstuvwxyz
16+
Cipher: nopqrstuvwxyzabcdefghijklm
17+
```
18+
19+
It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.
20+
21+
Ciphertext is written out in the same formatting as the input including spaces and punctuation.
22+
23+
## Examples
24+
25+
- ROT5 `omg` gives `trl`
26+
- ROT0 `c` gives `c`
27+
- ROT26 `Cool` gives `Cool`
28+
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
29+
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"root": true,
3+
"extends": "@exercism/eslint-config-javascript",
4+
"env": {
5+
"jest": true
6+
},
7+
"overrides": [
8+
{
9+
"files": [
10+
"*.spec.js"
11+
],
12+
"excludedFiles": [
13+
"custom.spec.js"
14+
],
15+
"extends": "@exercism/eslint-config-javascript/maintainers"
16+
}
17+
]
18+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"authors": [
3+
"kahgoh"
4+
],
5+
"files": {
6+
"solution": [
7+
"rotational-cipher.wat"
8+
],
9+
"test": [
10+
"rotational-cipher.spec.js"
11+
],
12+
"example": [
13+
".meta/proof.ci.wat"
14+
]
15+
},
16+
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
17+
"source": "Wikipedia",
18+
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher",
19+
"custom": {
20+
"version.tests.compatibility": "jest-27",
21+
"flag.tests.task-per-describe": false,
22+
"flag.tests.may-run-long": false,
23+
"flag.tests.includes-optional": false
24+
}
25+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
(module
2+
(memory (export "mem") 1)
3+
4+
(global $A i32 (i32.const 65))
5+
(global $Z i32 (i32.const 90))
6+
(global $a i32 (i32.const 97))
7+
(global $z i32 (i32.const 122))
8+
9+
(func $wrap (param $low i32) (param $high i32) (param $value i32) (param $shiftKey i32) (result i32)
10+
(local $newValue i32)
11+
(local.set $newValue (local.get $value))
12+
13+
(if
14+
(i32.ge_u
15+
(local.get $value)
16+
(local.get $low)
17+
)
18+
(then
19+
(if
20+
(i32.le_u
21+
(local.get $value)
22+
(local.get $high)
23+
)
24+
(then
25+
;; shift the value
26+
(local.set
27+
$newValue
28+
(i32.add
29+
(local.get $value)
30+
(local.get $shiftKey)
31+
)
32+
)
33+
34+
;; wrap when value greater than $high
35+
(if (i32.gt_u (local.get $newValue) (local.get $high))
36+
(then
37+
(local.set $newValue
38+
(i32.sub
39+
(local.get $newValue)
40+
(i32.const 26)
41+
)
42+
)
43+
)
44+
)
45+
)
46+
)
47+
)
48+
)
49+
50+
(return (local.get $newValue))
51+
)
52+
53+
(func (export "rotate") (param $textOffset i32) (param $textLength i32) (param $shiftKey i32) (result i32 i32)
54+
55+
(local $index i32)
56+
(local $stop i32)
57+
(local $value i32)
58+
59+
(local.set $index (local.get $textOffset))
60+
(local.set $stop
61+
(i32.add
62+
(local.get $textOffset)
63+
(local.get $textLength)
64+
)
65+
)
66+
67+
(loop $process
68+
;; shift upper case
69+
(local.set $value
70+
(call $wrap
71+
(global.get $A)
72+
(global.get $Z)
73+
(i32.load8_u (local.get $index))
74+
(local.get $shiftKey)
75+
)
76+
)
77+
78+
;; shift lower case
79+
(local.set $value
80+
(call $wrap
81+
(global.get $a)
82+
(global.get $z)
83+
(local.get $value)
84+
(local.get $shiftKey)
85+
)
86+
)
87+
88+
(i32.store8
89+
(local.get $index)
90+
(local.get $value)
91+
)
92+
93+
(local.set $index
94+
(i32.add (local.get $index) (i32.const 1))
95+
)
96+
97+
(br_if
98+
$process
99+
(i32.lt_u (local.get $index) (local.get $stop))
100+
)
101+
)
102+
103+
(return (local.get $textOffset) (local.get $textLength))
104+
)
105+
)
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
[74e58a38-e484-43f1-9466-877a7515e10f]
13+
description = "rotate a by 0, same output as input"
14+
15+
[7ee352c6-e6b0-4930-b903-d09943ecb8f5]
16+
description = "rotate a by 1"
17+
18+
[edf0a733-4231-4594-a5ee-46a4009ad764]
19+
description = "rotate a by 26, same output as input"
20+
21+
[e3e82cb9-2a5b-403f-9931-e43213879300]
22+
description = "rotate m by 13"
23+
24+
[19f9eb78-e2ad-4da4-8fe3-9291d47c1709]
25+
description = "rotate n by 13 with wrap around alphabet"
26+
27+
[a116aef4-225b-4da9-884f-e8023ca6408a]
28+
description = "rotate capital letters"
29+
30+
[71b541bb-819c-4dc6-a9c3-132ef9bb737b]
31+
description = "rotate spaces"
32+
33+
[ef32601d-e9ef-4b29-b2b5-8971392282e6]
34+
description = "rotate numbers"
35+
36+
[32dd74f6-db2b-41a6-b02c-82eb4f93e549]
37+
description = "rotate punctuation"
38+
39+
[9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9]
40+
description = "rotate all letters"
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
audit=false
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License
2+
3+
Copyright (c) 2021 Exercism
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export default {
2+
presets: ["@exercism/babel-preset-javascript"],
3+
plugins: [],
4+
};
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
{
2+
"name": "@exercism/wasm-rotational-cipher",
3+
"description": "Exercism exercises in WebAssembly.",
4+
"author": "Kah Goh",
5+
"type": "module",
6+
"private": true,
7+
"license": "MIT",
8+
"repository": {
9+
"type": "git",
10+
"url": "https://github.com/exercism/wasm",
11+
"directory": "exercises/practice/rotational-cipher"
12+
},
13+
"jest": {
14+
"maxWorkers": 1
15+
},
16+
"devDependencies": {
17+
"@babel/core": "^7.20.12",
18+
"@exercism/babel-preset-javascript": "^0.2.1",
19+
"@exercism/eslint-config-javascript": "^0.6.0",
20+
"@types/jest": "^29.4.0",
21+
"@types/node": "^18.13.0",
22+
"babel-jest": "^29.4.2",
23+
"core-js": "^3.27.2",
24+
"eslint": "^8.34.0",
25+
"jest": "^29.4.2"
26+
},
27+
"dependencies": {
28+
"@exercism/wasm-lib": "^0.1.0"
29+
},
30+
"scripts": {
31+
"test": "node --experimental-vm-modules node_modules/jest/bin/jest.js ./*",
32+
"watch": "node --experimental-vm-modules node_modules/jest/bin/jest.js --watch ./*",
33+
"lint": "eslint ."
34+
}
35+
}

0 commit comments

Comments
 (0)