Skip to content

Commit

Permalink
Add atbash-cipher (#274)
Browse files Browse the repository at this point in the history
* Add atbash-cipher

* Apply code review

* Fix formatting
  • Loading branch information
BNAndras authored Sep 12, 2024
1 parent f462907 commit a542481
Show file tree
Hide file tree
Showing 8 changed files with 331 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "968cab5e-d672-4975-96f1-2d600f9e9ec8",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "collatz-conjecture",
"name": "Collatz Conjecture",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/atbash-cipher/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.

The Atbash cipher is a simple substitution cipher that relies on transposing all the letters in the alphabet such that the resulting alphabet is backwards.
The first letter is replaced with the last letter, the second with the second-last, and so on.

An Atbash cipher for the Latin alphabet would be as follows:

```text
Plain: abcdefghijklmnopqrstuvwxyz
Cipher: zyxwvutsrqponmlkjihgfedcba
```

It is a very weak cipher because it only has one possible key, and it is a simple mono-alphabetic substitution cipher.
However, this may not have been an issue in the cipher's time.

Ciphertext is written out in groups of fixed length, the traditional group size being 5 letters, leaving numbers unchanged, and punctuation is excluded.
This is to make it harder to guess things based on word boundaries.
All text will be encoded as lowercase letters.

## Examples

- Encoding `test` gives `gvhg`
- Encoding `x123 yes` gives `c123b vh`
- Decoding `gvhg` gives `test`
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`
22 changes: 22 additions & 0 deletions exercises/practice/atbash-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"src/lib.cairo"
],
"test": [
"tests/atbash_cipher.cairo"
],
"example": [
".meta/example.cairo"
],
"invalidator": [
"Scarb.toml"
]
},
"blurb": "Create an implementation of the atbash cipher, an ancient encryption system created in the Middle East.",
"source": "Wikipedia",
"source_url": "https://en.wikipedia.org/wiki/Atbash"
}
78 changes: 78 additions & 0 deletions exercises/practice/atbash-cipher/.meta/example.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
const DISTANCE_BETWEEN_CASES: u8 = 'a' - 'A';
const MAX_ENCODED_CHAR: u8 = 'a' + 'z';

pub fn encode(phrase: ByteArray) -> ByteArray {
let mut result = "";
let mut chunk_size = 0;
let mut i = 0;
while i < phrase.len() {
let chr = phrase[i];
if is_alphanumeric(chr) {
if chunk_size == 5 {
result += " ";
chunk_size = 0;
}
let encoded = encode_char(chr);
result.append_byte(encoded);
chunk_size += 1;
}
i += 1;
};

result
}


pub fn decode(phrase: ByteArray) -> ByteArray {
let mut result = "";
let mut i = 0;
while i < phrase.len() {
let chr = phrase[i];
if !is_whitespace(chr) {
let decoded = encode_char(chr);
result.append_byte(decoded);
}
i += 1;
};
result
}

fn encode_char(chr: u8) -> u8 {
if is_letter(chr) {
MAX_ENCODED_CHAR - lowercase(chr)
} else {
chr
}
}

fn is_alphanumeric(chr: u8) -> bool {
is_letter(chr) || is_numeric(chr)
}

fn is_numeric(chr: u8) -> bool {
chr >= '0' && chr <= '9'
}

fn is_letter(chr: u8) -> bool {
is_lower(chr) || is_upper(chr)
}

fn is_lower(chr: u8) -> bool {
chr >= 'a' && chr <= 'z'
}

fn is_upper(chr: u8) -> bool {
chr >= 'A' && chr <= 'Z'
}

fn is_whitespace(chr: u8) -> bool {
chr == ' ' || chr == '\t' || chr == '\n' || chr == '\r'
}

fn lowercase(chr: u8) -> u8 {
if is_upper(chr) {
chr + DISTANCE_BETWEEN_CASES
} else {
chr
}
}
52 changes: 52 additions & 0 deletions exercises/practice/atbash-cipher/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# 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.

[2f47ebe1-eab9-4d6b-b3c6-627562a31c77]
description = "encode -> encode yes"

[b4ffe781-ea81-4b74-b268-cc58ba21c739]
description = "encode -> encode no"

[10e48927-24ab-4c4d-9d3f-3067724ace00]
description = "encode -> encode OMG"

[d59b8bc3-509a-4a9a-834c-6f501b98750b]
description = "encode -> encode spaces"

[31d44b11-81b7-4a94-8b43-4af6a2449429]
description = "encode -> encode mindblowingly"

[d503361a-1433-48c0-aae0-d41b5baa33ff]
description = "encode -> encode numbers"

[79c8a2d5-0772-42d4-b41b-531d0b5da926]
description = "encode -> encode deep thought"

[9ca13d23-d32a-4967-a1fd-6100b8742bab]
description = "encode -> encode all the letters"

[bb50e087-7fdf-48e7-9223-284fe7e69851]
description = "decode -> decode exercism"

[ac021097-cd5d-4717-8907-b0814b9e292c]
description = "decode -> decode a sentence"

[18729de3-de74-49b8-b68c-025eaf77f851]
description = "decode -> decode numbers"

[0f30325f-f53b-415d-ad3e-a7a4f63de034]
description = "decode -> decode all the letters"

[39640287-30c6-4c8c-9bac-9d613d1a5674]
description = "decode -> decode with too many spaces"

[b34edf13-34c0-49b5-aa21-0768928000d5]
description = "decode -> decode with no spaces"
7 changes: 7 additions & 0 deletions exercises/practice/atbash-cipher/Scarb.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "atbash_cipher"
version = "0.1.0"
edition = "2024_07"

[dev-dependencies]
cairo_test = "2.7.1"
7 changes: 7 additions & 0 deletions exercises/practice/atbash-cipher/src/lib.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
pub fn encode(phrase: ByteArray) -> ByteArray {
panic!("implement `encode`")
}

pub fn decode(phrase: ByteArray) -> ByteArray {
panic!("implement `decode`")
}
130 changes: 130 additions & 0 deletions exercises/practice/atbash-cipher/tests/atbash_cipher.cairo
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
use atbash_cipher::{encode, decode};

// Encode tests

#[test]
fn encode_yes() {
let phrase = "yes";
let output = encode(phrase);
let expected = "bvh";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_no() {
let phrase = "no";
let output = encode(phrase);
let expected = "ml";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_omg() {
let phrase = "OMG";
let output = encode(phrase);
let expected = "lnt";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_spaces() {
let phrase = "O M G";
let output = encode(phrase);
let expected = "lnt";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_mindblowingly() {
let phrase = "mindblowingly";
let output = encode(phrase);
let expected = "nrmwy oldrm tob";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_numbers() {
let phrase = "Testing, 1 2 3, testing.";
let output = encode(phrase);
let expected = "gvhgr mt123 gvhgr mt";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_deep_thought() {
let phrase = "Truth is fiction.";
let output = encode(phrase);
let expected = "gifgs rhurx grlm";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn encode_all_the_letters() {
let phrase = "The quick brown fox jumps over the lazy dog.";
let output = encode(phrase);
let expected = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt";
assert_eq!(expected, output);
}

// Decode tests

#[test]
#[ignore]
fn decode_exercism() {
let phrase = "vcvix rhn";
let output = decode(phrase);
let expected = "exercism";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn decode_a_sentence() {
let phrase = "zmlyh gzxov rhlug vmzhg vkkrm thglm v";
let output = decode(phrase);
let expected = "anobstacleisoftenasteppingstone";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn decode_numbers() {
let phrase = "gvhgr mt123 gvhgr mt";
let output = decode(phrase);
let expected = "testing123testing";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn decode_all_the_letters() {
let phrase = "gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt";
let output = decode(phrase);
let expected = "thequickbrownfoxjumpsoverthelazydog";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn decode_with_too_many_spaces() {
let phrase = "vc vix r hn";
let output = decode(phrase);
let expected = "exercism";
assert_eq!(expected, output);
}

#[test]
#[ignore]
fn decode_with_no_spaces() {
let phrase = "zmlyhgzxovrhlugvmzhgvkkrmthglmv";
let output = decode(phrase);
let expected = "anobstacleisoftenasteppingstone";
assert_eq!(expected, output);
}

0 comments on commit a542481

Please sign in to comment.