Skip to content

Commit 4c19fe2

Browse files
committed
add rotational-cipher
1 parent d5f36cd commit 4c19fe2

File tree

9 files changed

+179
-0
lines changed

9 files changed

+179
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@
170170
"prerequisites": [],
171171
"difficulty": 2
172172
},
173+
{
174+
"slug": "rotational-cipher",
175+
"name": "Rotational Cipher",
176+
"uuid": "365fb81f-c059-4231-954b-bac1878e2a0c",
177+
"practices": [],
178+
"prerequisites": [],
179+
"difficulty": 2
180+
},
173181
{
174182
"slug": "rna-transcription",
175183
"name": "RNA Transcription",
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
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: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"rotational_cipher.moon"
8+
],
9+
"test": [
10+
"rotational_cipher_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
rotate: (text, shift_key) ->
3+
text\gsub '.', (char) ->
4+
byte = string.byte char
5+
if byte >= 97 and byte <= 122
6+
string.char (byte - 97 + shift_key) % 26 + 97
7+
elseif byte >= 65 and byte <= 90
8+
string.char (byte - 65 + shift_key) % 26 + 65
9+
else
10+
char
11+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
module_name: 'RotationalCipher',
3+
4+
generate_test: (case, level) ->
5+
lines = {
6+
"result = RotationalCipher.rotate #{quote case.input.text}, #{case.input.shiftKey}",
7+
"expected = #{quote case.expected}",
8+
"assert.are.equal expected, result"
9+
}
10+
table.concat [indent line, level for line in *lines], '\n'
11+
}
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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
rotate: (text, shift_key) ->
3+
error 'Implement me'
4+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
RotationalCipher = require 'rotational_cipher'
2+
3+
describe 'rotational-cipher', ->
4+
it 'rotate a by 0, same output as input', ->
5+
result = RotationalCipher.rotate 'a', 0
6+
expected = 'a'
7+
assert.are.equal expected, result
8+
9+
pending 'rotate a by 1', ->
10+
result = RotationalCipher.rotate 'a', 1
11+
expected = 'b'
12+
assert.are.equal expected, result
13+
14+
pending 'rotate a by 26, same output as input', ->
15+
result = RotationalCipher.rotate 'a', 26
16+
expected = 'a'
17+
assert.are.equal expected, result
18+
19+
pending 'rotate m by 13', ->
20+
result = RotationalCipher.rotate 'm', 13
21+
expected = 'z'
22+
assert.are.equal expected, result
23+
24+
pending 'rotate n by 13 with wrap around alphabet', ->
25+
result = RotationalCipher.rotate 'n', 13
26+
expected = 'a'
27+
assert.are.equal expected, result
28+
29+
pending 'rotate capital letters', ->
30+
result = RotationalCipher.rotate 'OMG', 5
31+
expected = 'TRL'
32+
assert.are.equal expected, result
33+
34+
pending 'rotate spaces', ->
35+
result = RotationalCipher.rotate 'O M G', 5
36+
expected = 'T R L'
37+
assert.are.equal expected, result
38+
39+
pending 'rotate numbers', ->
40+
result = RotationalCipher.rotate 'Testing 1 2 3 testing', 4
41+
expected = 'Xiwxmrk 1 2 3 xiwxmrk'
42+
assert.are.equal expected, result
43+
44+
pending 'rotate punctuation', ->
45+
result = RotationalCipher.rotate "Let's eat, Grandma!", 21
46+
expected = "Gzo'n zvo, Bmviyhv!"
47+
assert.are.equal expected, result
48+
49+
pending 'rotate all letters', ->
50+
result = RotationalCipher.rotate 'The quick brown fox jumps over the lazy dog.', 13
51+
expected = 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt.'
52+
assert.are.equal expected, result

0 commit comments

Comments
 (0)