Skip to content

Commit

Permalink
Add rotational-cipher exercise (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Oct 21, 2024
1 parent a469dda commit ce9af72
Show file tree
Hide file tree
Showing 13 changed files with 3,592 additions and 0 deletions.
4 changes: 4 additions & 0 deletions bin/create-exercise
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ fi

slug=$1

snake="${slug//-/_}"

existing=$( jq --arg slug "${slug}" '.exercises.practice[] | select(.slug == $slug)' config.json )
if [[ -n ${existing} ]]; then
die "${slug} already exists in config.json"
Expand All @@ -34,3 +36,5 @@ bin/configlet create --practice-exercise "${slug}" --author "${author}" --diffic

cp exercises/practice/hello-world/Makefile exercises/practice/${slug}/Makefile
cp -r exercises/practice/hello-world/vendor exercises/practice/${slug}/vendor

touch generators/exercises/${snake}.py
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,14 @@
"prerequisites": [],
"difficulty": 4
},
{
"slug": "rotational-cipher",
"name": "Rotational Cipher",
"uuid": "efec64f7-3085-4afe-a8be-9e48e533550d",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "all-your-base",
"name": "All Your Base",
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": [
"keiravillekode"
],
"files": {
"solution": [
"rotational_cipher.s"
],
"test": [
"rotational_cipher_test.c"
],
"example": [
".meta/example.s"
]
},
"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"
}
27 changes: 27 additions & 0 deletions exercises/practice/rotational-cipher/.meta/example.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
.text
.globl rotate

/* extern void rotate(char *buffer, const char *text, int shift_key); */
rotate:

.read:
ldrb w4, [x1], #1 /* load byte, post-increment */
and w7, w4, #32 /* 32 if lower case, 0 if upper case */
sub w5, w4, w7 /* force upper case */
sub w5, w5, 65 /* subtract 'A' to obtain index in alphabet */
cmp w5, #26
bhs .write /* unsigned >= */

add w5, w5, 65 /* 'A' */
add w5, w5, w2 /* shift */
sub w6, w5, #26
cmp w5, #90 /* 'Z' */
csel w5, w5, w6, le /* w5 if <= 'Z', otherwise w5 - 26 */

orr w4, w5, w7 /* rotated letter, with original case */

.write:
strb w4, [x0], #1 /* store byte, post-increment */
cbnz w4, .read

ret
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"
36 changes: 36 additions & 0 deletions exercises/practice/rotational-cipher/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
AS = aarch64-linux-gnu-as
CC = aarch64-linux-gnu-gcc

CFLAGS = -g -Wall -Wextra -pedantic -Werror
LDFLAGS =

ALL_LDFLAGS = -pie -Wl,--fatal-warnings

ALL_CFLAGS = -std=c99 -fPIE $(CFLAGS)
ALL_LDFLAGS += $(LDFLAGS)

C_OBJS = $(patsubst %.c,%.o,$(wildcard *.c))
AS_OBJS = $(patsubst %.s,%.o,$(wildcard *.s))
ALL_OBJS = $(filter-out example.o,$(C_OBJS) $(AS_OBJS) vendor/unity.o)

CC_CMD = $(CC) $(ALL_CFLAGS) -c -o $@ $<

all: tests
qemu-aarch64 -L /usr/aarch64-linux-gnu ./$<

tests: $(ALL_OBJS)
@$(CC) $(ALL_CFLAGS) $(ALL_LDFLAGS) -o $@ $(ALL_OBJS)

%.o: %.s
@$(AS) -o $@ $<

%.o: %.c
@$(CC_CMD)

vendor/unity.o: vendor/unity.c vendor/unity.h vendor/unity_internals.h
@$(CC_CMD)

clean:
@rm -f *.o vendor/*.o tests

.PHONY: all clean
5 changes: 5 additions & 0 deletions exercises/practice/rotational-cipher/rotational_cipher.s
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
.text
.globl rotate

rotate:
ret
105 changes: 105 additions & 0 deletions exercises/practice/rotational-cipher/rotational_cipher_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#include "vendor/unity.h"

#define BUFFER_SIZE 80

extern void rotate(char *buffer, const char *text, int shift_key);

void setUp(void) {
}

void tearDown(void) {
}

void test_rotate_a_by_0_same_output_as_input(void) {
char buffer[BUFFER_SIZE];

rotate(buffer, "a", 0);
TEST_ASSERT_EQUAL_STRING("a", buffer);
}

void test_rotate_a_by_1(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "a", 1);
TEST_ASSERT_EQUAL_STRING("b", buffer);
}

void test_rotate_a_by_26_same_output_as_input(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "a", 26);
TEST_ASSERT_EQUAL_STRING("a", buffer);
}

void test_rotate_m_by_13(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "m", 13);
TEST_ASSERT_EQUAL_STRING("z", buffer);
}

void test_rotate_n_by_13_with_wrap_around_alphabet(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "n", 13);
TEST_ASSERT_EQUAL_STRING("a", buffer);
}

void test_rotate_capital_letters(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "OMG", 5);
TEST_ASSERT_EQUAL_STRING("TRL", buffer);
}

void test_rotate_spaces(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "O M G", 5);
TEST_ASSERT_EQUAL_STRING("T R L", buffer);
}

void test_rotate_numbers(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "Testing 1 2 3 testing", 4);
TEST_ASSERT_EQUAL_STRING("Xiwxmrk 1 2 3 xiwxmrk", buffer);
}

void test_rotate_punctuation(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "Let's eat, Grandma!", 21);
TEST_ASSERT_EQUAL_STRING("Gzo'n zvo, Bmviyhv!", buffer);
}

void test_rotate_all_letters(void) {
TEST_IGNORE();
char buffer[BUFFER_SIZE];

rotate(buffer, "The quick brown fox jumps over the lazy dog.", 13);
TEST_ASSERT_EQUAL_STRING("Gur dhvpx oebja sbk whzcf bire gur ynml qbt.", buffer);
}

int main(void) {
UNITY_BEGIN();
RUN_TEST(test_rotate_a_by_0_same_output_as_input);
RUN_TEST(test_rotate_a_by_1);
RUN_TEST(test_rotate_a_by_26_same_output_as_input);
RUN_TEST(test_rotate_m_by_13);
RUN_TEST(test_rotate_n_by_13_with_wrap_around_alphabet);
RUN_TEST(test_rotate_capital_letters);
RUN_TEST(test_rotate_spaces);
RUN_TEST(test_rotate_numbers);
RUN_TEST(test_rotate_punctuation);
RUN_TEST(test_rotate_all_letters);
return UNITY_END();
}
Loading

0 comments on commit ce9af72

Please sign in to comment.