Skip to content

Commit

Permalink
Add rotational-cipher (#257)
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras authored Jun 30, 2024
1 parent 527ea7d commit e9c534e
Show file tree
Hide file tree
Showing 11 changed files with 223 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
"uuid": "40ccdfb1-492f-4bba-917f-c40c75573ebf",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "scrabble-score",
"name": "Scrabble Score",
Expand Down
29 changes: 29 additions & 0 deletions exercises/practice/rotational-cipher/.docs/instructions.md
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.`
19 changes: 19 additions & 0 deletions exercises/practice/rotational-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/rotational-cipher.lfe"
],
"test": [
"test/rotational-cipher-tests.lfe"
],
"example": [
".meta/example.lfe"
]
},
"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"
}
24 changes: 24 additions & 0 deletions exercises/practice/rotational-cipher/.meta/example.lfe
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
(defmodule rotational-cipher
(export (rotate 2)))

(defun rotate (text shift)
(do-rotate text shift ""))

(defun do-rotate
(('() _ acc)
(unicode:characters_to_list (lists:reverse acc)))
(((cons head tail) shift acc)
(let ((chr (rotate-char head shift)))
(do-rotate tail shift (cons chr acc)))))

(defun rotate-char (chr shift)
(cond ((and (>= chr #\A) (=< chr #\Z))
(let* ((relative-offset (+ (- chr #\A) shift))
(codepoint (+ (rem relative-offset 26) #\A)))
codepoint))
((and (>= chr #\a) (=< chr #\z))
(let* ((relative-offset (+ (- chr #\a) shift))
(codepoint (+ (rem relative-offset 26) #\a)))
codepoint))
(else chr)))

40 changes: 40 additions & 0 deletions exercises/practice/rotational-cipher/.meta/tests.toml
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"
21 changes: 21 additions & 0 deletions exercises/practice/rotational-cipher/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
ERL := $(shell which erl)
REBAR3 := $(shell which rebar3)

null :=
space := $(null) #
comma := ,

ifeq ($(ERL),)
$(error Can't find Erlang executable 'erl')
else ifeq ($(REBAR3),)
$(error Can't find rebar3)
endif

compile: ; $(REBAR3) compile

clean: ; $(REBAR3) clean

.PHONY: test
test:
$(REBAR3) eunit \
-m $(subst $(space),$(comma),$(basename $(notdir $(wildcard test/*.lfe))))
11 changes: 11 additions & 0 deletions exercises/practice/rotational-cipher/rebar.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{plugins, [{rebar3_lfe, "0.4.10"}]}.

{provider_hooks, [{post, [{compile, {lfe, compile}}]}]}.

{deps, [{lfe, "2.1.3"}]}.

{profiles,
[{test,
[{eunit_compile_opts, [{src_dirs, ["src", "test"]}]},
{deps,
[{ltest, "0.13.8"}]}]}]}.
8 changes: 8 additions & 0 deletions exercises/practice/rotational-cipher/rebar.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{"1.2.0",
[{<<"lfe">>,{pkg,<<"lfe">>,<<"2.1.3">>},0}]}.
[
{pkg_hash,[
{<<"lfe">>, <<"6EFCB2BBC1FFC21DC5D1C092F00EFDB397EAC889474AC5C86EDF78A3557CC730">>}]},
{pkg_hash_ext,[
{<<"lfe">>, <<"4E4BAD515A169AE418FEB7374EA1C8D741FAEA9D95E266CE343B45BCC377F55B">>}]}
].
11 changes: 11 additions & 0 deletions exercises/practice/rotational-cipher/src/rotational-cipher.app.src
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
%% -*- erlang -*-
{application, 'rotational-cipher',
[{description, "exercism.org exercise: Rotational Cipher"},
{vsn, "0.0.1"},
{modules,
['rotational-cipher']},
{registered, []},
{applications,
[kernel, stdlib]},
{included_applications, []},
{env, []}]}.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
(defmodule rotational-cipher
(export (rotate 2)))

; Please implement the rotate function.

Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
(defmodule rotational-cipher-tests
(behaviour ltest-unit)
(export all))

(include-lib "ltest/include/ltest-macros.lfe")

(deftest rotate-a-by-0-same-output-as-input
(is-equal "a"
(rotational-cipher:rotate "a" 0)))

(deftest rotate-a-by-1
(is-equal "b"
(rotational-cipher:rotate "a" 1)))

(deftest rotate-a-by-26-same-output-as-input
(is-equal "a"
(rotational-cipher:rotate "a" 26)))

(deftest rotate-m-by-13
(is-equal "z"
(rotational-cipher:rotate "m" 13)))

(deftest rotate-n-by-13-with-wrap-around-alphabet
(is-equal "a"
(rotational-cipher:rotate "n" 13)))

(deftest rotate-capital-letters
(is-equal "TRL"
(rotational-cipher:rotate "OMG" 5)))

(deftest rotate-spaces
(is-equal "T R L"
(rotational-cipher:rotate "O M G" 5)))

(deftest rotate-numbers
(is-equal "Xiwxmrk 1 2 3 xiwxmrk"
(rotational-cipher:rotate "Testing 1 2 3 testing" 4)))

(deftest rotate-punctuation
(is-equal "Gzo'n zvo, Bmviyhv!"
(rotational-cipher:rotate "Let's eat, Grandma!" 21)))

(deftest rotate-all-letters
(is-equal "Gur dhvpx oebja sbk whzcf bire gur ynml qbt."
(rotational-cipher:rotate "The quick brown fox jumps over the lazy dog."
13)))

0 comments on commit e9c534e

Please sign in to comment.