-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add rotational-cipher exercise (#81)
- Loading branch information
1 parent
a469dda
commit ce9af72
Showing
13 changed files
with
3,592 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
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,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" | ||
} |
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,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 |
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" |
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,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 |
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,5 @@ | ||
.text | ||
.globl rotate | ||
|
||
rotate: | ||
ret |
105 changes: 105 additions & 0 deletions
105
exercises/practice/rotational-cipher/rotational_cipher_test.c
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,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(); | ||
} |
Oops, something went wrong.