Skip to content

Commit

Permalink
Exercise: rotational-cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
loziniak committed Nov 3, 2021
1 parent 514781c commit 6797084
Show file tree
Hide file tree
Showing 7 changed files with 271 additions and 0 deletions.
13 changes: 13 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,19 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
"staus": "beta",
"uuid": "139cfa97-ef3a-4f9d-8673-4d525c5c5aa0",
"practices": [
"conditionals"
],
"prerequisites": [
"strings"
],
"difficulty": 4
},
{
"slug": "roman-numerals",
"name": "Roman Numerals",
Expand Down
31 changes: 31 additions & 0 deletions exercises/practice/rotational-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Instructions

Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.

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`. Using a key of `0` or `26` will always yield
the same output due to modular arithmetic. The letter is shifted
for as many values as the value of the key.

The general notation for rotational ciphers is `ROT + <key>`.
The most commonly used rotational cipher is `ROT13`.

A `ROT13` on the Latin alphabet would be as follows:

```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: nopqrstuvwxyzabcdefghijklm
```

It is stronger than the Atbash cipher because it has 27 possible keys, and 25 usable keys.

Ciphertext is written out in the same formatting as the input including spaces and punctuation.

## Examples

- ROT5 `omg` gives `trl`
- ROT0 `c` gives `c`
- ROT26 `Cool` gives `Cool`
- ROT13 `The quick brown fox jumps over the lazy dog.` gives `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.`
- ROT13 `Gur dhvpx oebja sbk whzcf bire gur ynml qbt.` gives `The quick brown fox jumps over the lazy dog.`
18 changes: 18 additions & 0 deletions exercises/practice/rotational-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.",
"authors": [
"loziniak"
],
"contributors": [],
"files": {
"solution": [
"rotational-cipher.red"
],
"test": [
"rotational-cipher-test.red"
],
"example": [
".meta/example.red"
]
}
}
28 changes: 28 additions & 0 deletions exercises/practice/rotational-cipher/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
Red [
description: {"Rotational Cipher" exercise solution for exercism platform}
author: "loziniak"
]

rotation-code: function [start [char!]] [
compose/deep [
all [c >= (start) c < (start + 26)] [
c: c + shiftKey
if c >= (start + 26) [c: c - 26]
change text c
]
]
]

rotate: function [
text
shiftKey
] compose/deep [
forall text [
c: first text
case [
(rotation-code #"A")
(rotation-code #"a")
]
]
text
]
32 changes: 32 additions & 0 deletions exercises/practice/rotational-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
[canonical-tests]

# rotate a by 0, same output as input
"74e58a38-e484-43f1-9466-877a7515e10f" = true

# rotate a by 1
"7ee352c6-e6b0-4930-b903-d09943ecb8f5" = true

# rotate a by 26, same output as input
"edf0a733-4231-4594-a5ee-46a4009ad764" = true

# rotate m by 13
"e3e82cb9-2a5b-403f-9931-e43213879300" = true

# rotate n by 13 with wrap around alphabet
"19f9eb78-e2ad-4da4-8fe3-9291d47c1709" = true

# rotate capital letters
"a116aef4-225b-4da9-884f-e8023ca6408a" = true

# rotate spaces
"71b541bb-819c-4dc6-a9c3-132ef9bb737b" = true

# rotate numbers
"ef32601d-e9ef-4b29-b2b5-8971392282e6" = true

# rotate punctuation
"32dd74f6-db2b-41a6-b02c-82eb4f93e549" = true

# rotate all letters
"9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9" = true

137 changes: 137 additions & 0 deletions exercises/practice/rotational-cipher/rotational-cipher-test.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
Red [
description: {Tests for Rotational Cipher Exercism exercise}
author: "loziniak"
]

exercise-slug: "rotational-cipher"
ignore-after: 1


canonical-cases: [#(
description: "rotate a by 0, same output as input"
input: #(
text: "a"
shiftKey: 0
)
expected: "a"
function: "rotate"
uuid: "74e58a38-e484-43f1-9466-877a7515e10f"
) #(
description: "rotate a by 1"
input: #(
text: "a"
shiftKey: 1
)
expected: "b"
function: "rotate"
uuid: "7ee352c6-e6b0-4930-b903-d09943ecb8f5"
) #(
description: "rotate a by 26, same output as input"
input: #(
text: "a"
shiftKey: 26
)
expected: "a"
function: "rotate"
uuid: "edf0a733-4231-4594-a5ee-46a4009ad764"
) #(
description: "rotate m by 13"
input: #(
text: "m"
shiftKey: 13
)
expected: "z"
function: "rotate"
uuid: "e3e82cb9-2a5b-403f-9931-e43213879300"
) #(
description: "rotate n by 13 with wrap around alphabet"
input: #(
text: "n"
shiftKey: 13
)
expected: "a"
function: "rotate"
uuid: "19f9eb78-e2ad-4da4-8fe3-9291d47c1709"
) #(
description: "rotate capital letters"
input: #(
text: "OMG"
shiftKey: 5
)
expected: "TRL"
function: "rotate"
uuid: "a116aef4-225b-4da9-884f-e8023ca6408a"
) #(
description: "rotate spaces"
input: #(
text: "O M G"
shiftKey: 5
)
expected: "T R L"
function: "rotate"
uuid: "71b541bb-819c-4dc6-a9c3-132ef9bb737b"
) #(
description: "rotate numbers"
input: #(
text: "Testing 1 2 3 testing"
shiftKey: 4
)
expected: "Xiwxmrk 1 2 3 xiwxmrk"
function: "rotate"
uuid: "ef32601d-e9ef-4b29-b2b5-8971392282e6"
) #(
description: "rotate punctuation"
input: #(
text: "Let's eat, Grandma!"
shiftKey: 21
)
expected: "Gzo'n zvo, Bmviyhv!"
function: "rotate"
uuid: "32dd74f6-db2b-41a6-b02c-82eb4f93e549"
) #(
description: "rotate all letters"
input: #(
text: "The quick brown fox jumps over the lazy dog."
shiftKey: 13
)
expected: "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
function: "rotate"
uuid: "9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9"
)]



print ["Testing" ignore-after "cases…"]

cases: copy/deep/part canonical-cases ignore-after
foreach test-case cases [
result: context load to file!
rejoin [exercise-slug %.red]
; %.meta/example.red ; test example solution

; function name
result-execution: reduce [
make path! reduce [
'result
to word! test-case/function
]
]
; arguments
append result-execution values-of test-case/input

result: do result-execution

print [
pad/with test-case/description 30 #"."
either result = test-case/expected [
"✓"
] [
rejoin [{FAILED. Expected: "} test-case/expected {", but got "} result {"}]
]
]
]

print [
(length? canonical-cases) - ignore-after
"cases ignored."
]
12 changes: 12 additions & 0 deletions exercises/practice/rotational-cipher/rotational-cipher.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
Red [
description: {"Rotational Cipher" exercise solution for exercism platform}
author: "" ; you can write your name here, in quotes
]

rotate: function [
text
shiftKey
] [
cause-error 'user 'message ["You need to implement this function."]
]

0 comments on commit 6797084

Please sign in to comment.