Skip to content

Commit

Permalink
Add atbash-cipher
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Sep 22, 2024
1 parent 31d4267 commit db332b9
Show file tree
Hide file tree
Showing 9 changed files with 276 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,14 @@
"strings"
]
},
{
"slug": "atbash-cipher",
"name": "Atbash Cipher",
"uuid": "7093ae21-54ce-4cc8-87a8-777fae080b57",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "eliuds-eggs",
"name": "Eliud's Eggs",
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`
19 changes: 19 additions & 0 deletions exercises/practice/atbash-cipher/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"lib/atbash_cipher.dart"
],
"test": [
"test/atbash_cipher_test.dart"
],
"example": [
".meta/lib/example.dart"
]
},
"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"
}
59 changes: 59 additions & 0 deletions exercises/practice/atbash-cipher/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
class AtbashCipher {
final lowercaseA = 'a'.codeUnitAt(0);
final lowercaseZ = 'z'.codeUnitAt(0);
final uppercaseA = 'A'.codeUnitAt(0);
final uppercaseZ = 'Z'.codeUnitAt(0);

String encode(String input) {
final encoded = StringBuffer();
int count = 0;
input.split('').where((elt) => elt.isAlphanumeric()).forEach((elt) {
int char = elt.codeUnitAt(0);
if (count == 5) {
encoded.write(" ");
count = 0;
}

if (char.isAlphabetic()) {
char = char.isLowercase() ? char : char.toLowercase();
char = lowercaseA + lowercaseZ - char;
}

encoded.write(String.fromCharCode(char));
count += 1;
});

return encoded.toString();
}

String decode(String input) {
final encoded = StringBuffer();
input.split('').where((elt) => elt.isAlphanumeric()).forEach((elt) {
int char = elt.codeUnitAt(0);
if (char.isAlphabetic()) {
char = char.isLowercase() ? char : char.toLowercase();
char = lowercaseA + lowercaseZ - char;
}

encoded.write(String.fromCharCode(char));
});
return encoded.toString();
}
}

extension IntNum on int {
bool inRange(int lower, int upper) => this >= lower && this <= upper;
bool isUppercase() => inRange('A'.codeUnitAt(0), 'Z'.codeUnitAt(0));
bool isLowercase() => inRange('a'.codeUnitAt(0), 'z'.codeUnitAt(0));
bool isAlphabetic() => isUppercase() || isLowercase();
bool isNumeric() => this >= '0'.codeUnitAt(0) && this <= '9'.codeUnitAt(0);
bool isAlphanumeric() => isAlphabetic() || isNumeric();
bool isWhitespace() => this == ' '.codeUnitAt(0);
int toLowercase() => this + 'a'.codeUnitAt(0) - 'A'.codeUnitAt(0);
}

extension StringExt on String {
bool isWhitespace() => this.codeUnitAt(0).isWhitespace();
bool isNotWhitespace() => !isWhitespace();
bool isAlphanumeric() => this.codeUnitAt(0).isAlphanumeric();
}
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"
18 changes: 18 additions & 0 deletions exercises/practice/atbash-cipher/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
3 changes: 3 additions & 0 deletions exercises/practice/atbash-cipher/lib/atbash_cipher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
class AtbashCipher {
// Put your code here
}
5 changes: 5 additions & 0 deletions exercises/practice/atbash-cipher/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'atbash_cipher'
environment:
sdk: '>=3.2.0 <4.0.0'
dev_dependencies:
test: '<2.0.0'
85 changes: 85 additions & 0 deletions exercises/practice/atbash-cipher/test/atbash_cipher_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import 'package:atbash_cipher/atbash_cipher.dart';
import 'package:test/test.dart';

void main() {
final atbashCipher = AtbashCipher();

group('AtbashCipher', () {
group('encode', () {
test('encode yes', () {
final result = atbashCipher.encode("yes");
expect(result, equals("bvh"));
}, skip: false);

test('encode no', () {
final result = atbashCipher.encode("no");
expect(result, equals("ml"));
}, skip: true);

test('encode OMG', () {
final result = atbashCipher.encode("OMG");
expect(result, equals("lnt"));
}, skip: true);

test('encode spaces', () {
final result = atbashCipher.encode("O M G");
expect(result, equals("lnt"));
}, skip: true);

test('encode mindblowingly', () {
final result = atbashCipher.encode("mindblowingly");
expect(result, equals("nrmwy oldrm tob"));
}, skip: true);

test('encode numbers', () {
final result = atbashCipher.encode("Testing,1 2 3, testing.");
expect(result, equals("gvhgr mt123 gvhgr mt"));
}, skip: true);

test('encode deep thought', () {
final result = atbashCipher.encode("Truth is fiction.");
expect(result, equals("gifgs rhurx grlm"));
}, skip: true);

test('encode all the letters', () {
final result =
atbashCipher.encode("The quick brown fox jumps over the lazy dog.");
expect(result, equals("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt"));
}, skip: true);
});

group('decode', () {
test('decode exercism', () {
final result = atbashCipher.decode("vcvix rhn");
expect(result, equals("exercism"));
}, skip: true);

test('decode a sentence', () {
final result =
atbashCipher.decode("zmlyh gzxov rhlug vmzhg vkkrm thglm v");
expect(result, equals("anobstacleisoftenasteppingstone"));
}, skip: true);

test('decode numbers', () {
final result = atbashCipher.decode("gvhgr mt123 gvhgr mt");
expect(result, equals("testing123testing"));
}, skip: true);

test('decode all the letters', () {
final result =
atbashCipher.decode("gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt");
expect(result, equals("thequickbrownfoxjumpsoverthelazydog"));
}, skip: true);

test('decode with too many spaces', () {
final result = atbashCipher.decode("vc vix r hn");
expect(result, equals("exercism"));
}, skip: true);

test('decode with no spaces', () {
final result = atbashCipher.decode("zmlyhgzxovrhlugvmzhgvkkrmthglmv");
expect(result, equals("anobstacleisoftenasteppingstone"));
}, skip: true);
});
});
}

0 comments on commit db332b9

Please sign in to comment.