diff --git a/config.json b/config.json index 7be0b1d..db204c6 100644 --- a/config.json +++ b/config.json @@ -411,6 +411,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "rotational-cipher", + "name": "Rotational Cipher", + "uuid": "1c9c40a6-4af3-4691-9b15-554f4ccab5f1", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "scrabble-score", "name": "Scrabble Score", diff --git a/exercises/practice/rotational-cipher/.docs/instructions.md b/exercises/practice/rotational-cipher/.docs/instructions.md new file mode 100644 index 0000000..4bf64ca --- /dev/null +++ b/exercises/practice/rotational-cipher/.docs/instructions.md @@ -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 + `. +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.` diff --git a/exercises/practice/rotational-cipher/.meta/config.json b/exercises/practice/rotational-cipher/.meta/config.json new file mode 100644 index 0000000..b87e185 --- /dev/null +++ b/exercises/practice/rotational-cipher/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "rotational-cipher.arr" + ], + "test": [ + "rotational-cipher-test.arr" + ], + "example": [ + ".meta/example.arr" + ] + }, + "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" +} diff --git a/exercises/practice/rotational-cipher/.meta/example.arr b/exercises/practice/rotational-cipher/.meta/example.arr new file mode 100644 index 0000000..403167b --- /dev/null +++ b/exercises/practice/rotational-cipher/.meta/example.arr @@ -0,0 +1,16 @@ +use context starter2024 + +provide: my-rotate end + +fun my-rotate(phrase, shift-key): + string-explode(phrase).map(lam(char): + code = string-to-code-point(char) + ask: + | (97 <= code) and (code <= 122) then: + string-from-code-point(num-modulo(((code - 97) + shift-key), 26) + 97) + | (65 <= code) and (code <= 90) then: + string-from-code-point(num-modulo(((code - 65) + shift-key), 26) + 65) + | otherwise: char + end + end).join-str("") +end diff --git a/exercises/practice/rotational-cipher/.meta/tests.toml b/exercises/practice/rotational-cipher/.meta/tests.toml new file mode 100644 index 0000000..53441ed --- /dev/null +++ b/exercises/practice/rotational-cipher/.meta/tests.toml @@ -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" diff --git a/exercises/practice/rotational-cipher/rotational-cipher-test.arr b/exercises/practice/rotational-cipher/rotational-cipher-test.arr new file mode 100644 index 0000000..155bf56 --- /dev/null +++ b/exercises/practice/rotational-cipher/rotational-cipher-test.arr @@ -0,0 +1,43 @@ +use context starter2024 + +include file("rotational-cipher.arr") + +check "rotate a by 0, same output as input": + my-rotate("a", 0) is "a" +end + +check "rotate a by 1": + my-rotate("a", 1) is "b" +end + +check "rotate a by 26, same output as input": + my-rotate("a", 26) is "a" +end + +check "rotate m by 13": + my-rotate("m", 13) is "z" +end + +check "rotate n by 13 with wrap around alphabet": + my-rotate("n", 13) is "a" +end + +check "rotate capital letters": + my-rotate("OMG", 5) is "TRL" +end + +check "rotate spaces": + my-rotate("O M G", 5) is "T R L" +end + +check "rotate numbers": + my-rotate("Testing 1 2 3 testing", 4) is "Xiwxmrk 1 2 3 xiwxmrk" +end + +check "rotate punctuation": + my-rotate("Let's eat, Grandma!", 21) is "Gzo'n zvo, Bmviyhv!" +end + +check "rotate all letters": + my-rotate("The quick brown fox jumps over the lazy dog.", 13) is "Gur dhvpx oebja sbk whzcf bire gur ynml qbt." +end diff --git a/exercises/practice/rotational-cipher/rotational-cipher.arr b/exercises/practice/rotational-cipher/rotational-cipher.arr new file mode 100644 index 0000000..462ec6b --- /dev/null +++ b/exercises/practice/rotational-cipher/rotational-cipher.arr @@ -0,0 +1,7 @@ +use context starter2024 + +provide: my-rotate end + +fun my-rotate(phrase, shift-key): + raise("Please implement the my-rotate function") +end