-
-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[rotational-cipher] Implementation (#673)
- Loading branch information
1 parent
e144a6c
commit ec5c59a
Showing
9 changed files
with
260 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
exercises/practice/rotational-cipher/.docs/instructions.md
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
# 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.` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"authors": [ | ||
"habere-et-dispertire" | ||
], | ||
"contributors": [ | ||
"m-dango" | ||
], | ||
"files": { | ||
"solution": [ | ||
"RotationalCipher.rakumod" | ||
], | ||
"test": [ | ||
"rotational-cipher.rakutest" | ||
], | ||
"example": [ | ||
".meta/solutions/RotationalCipher.rakumod" | ||
] | ||
}, | ||
"blurb": "Create an implementation of the rotational cipher, also sometimes called the Caesar cipher.", | ||
"source": "Wikipedia", | ||
"source_url": "https://en.wikipedia.org/wiki/Caesar_cipher" | ||
} |
6 changes: 6 additions & 0 deletions
6
exercises/practice/rotational-cipher/.meta/solutions/RotationalCipher.rakumod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
unit module RotationalCipher; | ||
|
||
sub caesar-cipher ( Str :$text, Int :$shift-key --> Str ) is export { | ||
$text.trans( 'A'..'Z' => rotate ( 'A'..'Z' ).Slip, $shift-key ) | ||
.trans: 'a'..'z' => rotate ( 'a'..'z' ).Slip, $shift-key; | ||
} |
1 change: 1 addition & 0 deletions
1
exercises/practice/rotational-cipher/.meta/solutions/rotational-cipher.rakutest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../../rotational-cipher.rakutest |
46 changes: 46 additions & 0 deletions
46
exercises/practice/rotational-cipher/.meta/template-data.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
unit: module | ||
|
||
properties: | ||
rotate: | ||
test: |- | ||
my Str:D $template.=new; | ||
if %case<input><shiftKey> != 13 { | ||
$template = q:to/END/; | ||
cmp-ok( | ||
caesar-cipher( :text(%s), :shift-key(%s) ), | ||
"eq", | ||
%s, | ||
%s, | ||
); | ||
END | ||
} | ||
else { | ||
$template = q:to/END/; | ||
subtest %4$s => { | ||
cmp-ok( | ||
caesar-cipher( :text(%1$s), :shift-key(%2$s) ), | ||
"eq", | ||
%3$s, | ||
"encrypt", | ||
); | ||
cmp-ok( | ||
caesar-cipher( :text(%3$s), :shift-key(%2$s) ), | ||
"eq", | ||
%1$s, | ||
"decrypt", | ||
); | ||
} | ||
END | ||
} | ||
sprintf($template, %case<input><text>.raku, %case<input><shiftKey>, %case<expected>.raku, %case<description>.raku); | ||
example: |- | ||
sub caesar-cipher ( Str :$text, Int :$shift-key --> Str ) is export { | ||
$text.trans( 'A'..'Z' => rotate ( 'A'..'Z' ).Slip, $shift-key ) | ||
.trans: 'a'..'z' => rotate ( 'a'..'z' ).Slip, $shift-key; | ||
} | ||
stub: |- | ||
sub caesar-cipher ( :$text, :$shift-key ) is export { | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# 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. | ||
|
||
[74e58a38-e484-43f1-9466-877a7515e10f] | ||
description = "rotate a by 0, same output as input" | ||
|
||
[7ee352c6-e6b0-4930-b903-d09943ecb8f5] | ||
description = "rotate a by 1" | ||
|
||
[edf0a733-4231-4594-a5ee-46a4009ad764] | ||
description = "rotate a by 26, same output as input" | ||
|
||
[e3e82cb9-2a5b-403f-9931-e43213879300] | ||
description = "rotate m by 13" | ||
|
||
[19f9eb78-e2ad-4da4-8fe3-9291d47c1709] | ||
description = "rotate n by 13 with wrap around alphabet" | ||
|
||
[a116aef4-225b-4da9-884f-e8023ca6408a] | ||
description = "rotate capital letters" | ||
|
||
[71b541bb-819c-4dc6-a9c3-132ef9bb737b] | ||
description = "rotate spaces" | ||
|
||
[ef32601d-e9ef-4b29-b2b5-8971392282e6] | ||
description = "rotate numbers" | ||
|
||
[32dd74f6-db2b-41a6-b02c-82eb4f93e549] | ||
description = "rotate punctuation" | ||
|
||
[9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9] | ||
description = "rotate all letters" |
4 changes: 4 additions & 0 deletions
4
exercises/practice/rotational-cipher/RotationalCipher.rakumod
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
unit module RotationalCipher; | ||
|
||
sub caesar-cipher ( :$text, :$shift-key ) is export { | ||
} |
103 changes: 103 additions & 0 deletions
103
exercises/practice/rotational-cipher/rotational-cipher.rakutest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
#!/usr/bin/env raku | ||
use Test; | ||
use lib $?FILE.IO.dirname; | ||
use RotationalCipher; | ||
|
||
cmp-ok( # begin: 74e58a38-e484-43f1-9466-877a7515e10f | ||
caesar-cipher( :text("a"), :shift-key(0) ), | ||
"eq", | ||
"a", | ||
"rotate a by 0, same output as input", | ||
); # end: 74e58a38-e484-43f1-9466-877a7515e10f | ||
|
||
cmp-ok( # begin: 7ee352c6-e6b0-4930-b903-d09943ecb8f5 | ||
caesar-cipher( :text("a"), :shift-key(1) ), | ||
"eq", | ||
"b", | ||
"rotate a by 1", | ||
); # end: 7ee352c6-e6b0-4930-b903-d09943ecb8f5 | ||
|
||
cmp-ok( # begin: edf0a733-4231-4594-a5ee-46a4009ad764 | ||
caesar-cipher( :text("a"), :shift-key(26) ), | ||
"eq", | ||
"a", | ||
"rotate a by 26, same output as input", | ||
); # end: edf0a733-4231-4594-a5ee-46a4009ad764 | ||
|
||
subtest "rotate m by 13" => { # begin: e3e82cb9-2a5b-403f-9931-e43213879300 | ||
cmp-ok( | ||
caesar-cipher( :text("m"), :shift-key(13) ), | ||
"eq", | ||
"z", | ||
"encrypt", | ||
); | ||
|
||
cmp-ok( | ||
caesar-cipher( :text("z"), :shift-key(13) ), | ||
"eq", | ||
"m", | ||
"decrypt", | ||
); | ||
} # end: e3e82cb9-2a5b-403f-9931-e43213879300 | ||
|
||
subtest "rotate n by 13 with wrap around alphabet" => { # begin: 19f9eb78-e2ad-4da4-8fe3-9291d47c1709 | ||
cmp-ok( | ||
caesar-cipher( :text("n"), :shift-key(13) ), | ||
"eq", | ||
"a", | ||
"encrypt", | ||
); | ||
|
||
cmp-ok( | ||
caesar-cipher( :text("a"), :shift-key(13) ), | ||
"eq", | ||
"n", | ||
"decrypt", | ||
); | ||
} # end: 19f9eb78-e2ad-4da4-8fe3-9291d47c1709 | ||
|
||
cmp-ok( # begin: a116aef4-225b-4da9-884f-e8023ca6408a | ||
caesar-cipher( :text("OMG"), :shift-key(5) ), | ||
"eq", | ||
"TRL", | ||
"rotate capital letters", | ||
); # end: a116aef4-225b-4da9-884f-e8023ca6408a | ||
|
||
cmp-ok( # begin: 71b541bb-819c-4dc6-a9c3-132ef9bb737b | ||
caesar-cipher( :text("O M G"), :shift-key(5) ), | ||
"eq", | ||
"T R L", | ||
"rotate spaces", | ||
); # end: 71b541bb-819c-4dc6-a9c3-132ef9bb737b | ||
|
||
cmp-ok( # begin: ef32601d-e9ef-4b29-b2b5-8971392282e6 | ||
caesar-cipher( :text("Testing 1 2 3 testing"), :shift-key(4) ), | ||
"eq", | ||
"Xiwxmrk 1 2 3 xiwxmrk", | ||
"rotate numbers", | ||
); # end: ef32601d-e9ef-4b29-b2b5-8971392282e6 | ||
|
||
cmp-ok( # begin: 32dd74f6-db2b-41a6-b02c-82eb4f93e549 | ||
caesar-cipher( :text("Let's eat, Grandma!"), :shift-key(21) ), | ||
"eq", | ||
"Gzo'n zvo, Bmviyhv!", | ||
"rotate punctuation", | ||
); # end: 32dd74f6-db2b-41a6-b02c-82eb4f93e549 | ||
|
||
subtest "rotate all letters" => { # begin: 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9 | ||
cmp-ok( | ||
caesar-cipher( :text("The quick brown fox jumps over the lazy dog."), :shift-key(13) ), | ||
"eq", | ||
"Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", | ||
"encrypt", | ||
); | ||
|
||
cmp-ok( | ||
caesar-cipher( :text("Gur dhvpx oebja sbk whzcf bire gur ynml qbt."), :shift-key(13) ), | ||
"eq", | ||
"The quick brown fox jumps over the lazy dog.", | ||
"decrypt", | ||
); | ||
} # end: 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9 | ||
|
||
done-testing; |