Skip to content

Commit 91704f4

Browse files
committed
sync atbash-cipher
1 parent 1df6486 commit 91704f4

File tree

3 files changed

+121
-58
lines changed

3 files changed

+121
-58
lines changed

exercises/practice/atbash-cipher/.docs/instructions.md

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

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

5-
The Atbash cipher is a simple substitution cipher that relies on
6-
transposing all the letters in the alphabet such that the resulting
7-
alphabet is backwards. The first letter is replaced with the last
8-
letter, the second with the second-last, and so on.
5+
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.
6+
The first letter is replaced with the last letter, the second with the second-last, and so on.
97

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

@@ -14,16 +12,16 @@ Plain: abcdefghijklmnopqrstuvwxyz
1412
Cipher: zyxwvutsrqponmlkjihgfedcba
1513
```
1614

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

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

2522
## Examples
2623

2724
- Encoding `test` gives `gvhg`
25+
- Encoding `x123 yes` gives `c123b vh`
2826
- Decoding `gvhg` gives `test`
2927
- Decoding `gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt` gives `thequickbrownfoxjumpsoverthelazydog`

exercises/practice/atbash-cipher/.meta/example.php

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
function encode($string)
Lines changed: 113 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,5 @@
11
<?php
22

3-
/*
4-
* By adding type hints and enabling strict type checking, code can become
5-
* easier to read, self-documenting and reduce the number of potential bugs.
6-
* By default, type declarations are non-strict, which means they will attempt
7-
* to change the original type to match the type specified by the
8-
* type-declaration.
9-
*
10-
* In other words, if you pass a string to a function requiring a float,
11-
* it will attempt to convert the string value to a float.
12-
*
13-
* To enable strict mode, a single declare directive must be placed at the top
14-
* of the file.
15-
* This means that the strictness of typing is configured on a per-file basis.
16-
* This directive not only affects the type declarations of parameters, but also
17-
* a function's return type.
18-
*
19-
* For more info review the Concept on strict type checking in the PHP track
20-
* <link>.
21-
*
22-
* To disable strict typing, comment out the directive below.
23-
*/
24-
253
declare(strict_types=1);
264

275
class AtbashCipherTest extends PHPUnit\Framework\TestCase
@@ -31,45 +9,154 @@ public static function setUpBeforeClass(): void
319
require_once 'AtbashCipher.php';
3210
}
3311

34-
public function testEncodeNo(): void
35-
{
36-
$this->assertEquals('ml', encode('no'));
37-
}
12+
/**
13+
* Encoding from English to atbash cipher
14+
*/
3815

16+
/**
17+
* uuid 2f47ebe1-eab9-4d6b-b3c6-627562a31c77
18+
* @testdox encode yes
19+
*/
3920
public function testEncodeYes(): void
4021
{
4122
$this->assertEquals('bvh', encode('yes'));
4223
}
4324

25+
/**
26+
* uuid b4ffe781-ea81-4b74-b268-cc58ba21c739
27+
* @testdox encode no
28+
*/
29+
public function testEncodeNo(): void
30+
{
31+
$this->assertEquals('ml', encode('no'));
32+
}
33+
34+
/**
35+
* uuid 10e48927-24ab-4c4d-9d3f-3067724ace00
36+
* @testdox encode OMG
37+
*/
4438
public function testEncodeOmg(): void
4539
{
4640
$this->assertEquals('lnt', encode('OMG'));
4741
}
4842

43+
/**
44+
* uuid d59b8bc3-509a-4a9a-834c-6f501b98750b
45+
* @testdox encode spaces
46+
*/
4947
public function testEncodeOmgWithSpaces(): void
5048
{
5149
$this->assertEquals('lnt', encode('O M G'));
5250
}
5351

52+
/**
53+
* uuid 31d44b11-81b7-4a94-8b43-4af6a2449429
54+
* @testdox encode mindblowingly
55+
*/
5456
public function testEncodeLongWord(): void
5557
{
5658
$this->assertEquals('nrmwy oldrm tob', encode('mindblowingly'));
5759
}
5860

61+
/**
62+
* uuid d503361a-1433-48c0-aae0-d41b5baa33ff
63+
* @testdox encode numbers
64+
*/
5965
public function testEncodeNumbers(): void
6066
{
6167
$this->assertEquals('gvhgr mt123 gvhgr mt', encode('Testing, 1 2 3, testing.'));
6268
}
6369

70+
/**
71+
* uuid 79c8a2d5-0772-42d4-b41b-531d0b5da926
72+
* @testdox encode deep thought
73+
*/
6474
public function testEncodeSentence(): void
6575
{
6676
$this->assertEquals('gifgs rhurx grlm', encode('Truth is fiction.'));
6777
}
6878

79+
/**
80+
* uuid 9ca13d23-d32a-4967-a1fd-6100b8742bab
81+
* @testdox encode all the letters
82+
*/
6983
public function testEncodeAllTheThings(): void
7084
{
7185
$plaintext = 'The quick brown fox jumps over the lazy dog.';
7286
$encoded = 'gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt';
7387
$this->assertEquals($encoded, encode($plaintext));
7488
}
89+
90+
/**
91+
* Decoding from atbash cipher to all-lowercase-mashed-together English
92+
*/
93+
94+
/**
95+
* uuid bb50e087-7fdf-48e7-9223-284fe7e69851
96+
* @testdox decode exercism
97+
*/
98+
public function testDecodeExercism(): void
99+
{
100+
$this->assertEquals('exercism', decode('vcvix rhn'));
101+
}
102+
103+
/**
104+
* uuid ac021097-cd5d-4717-8907-b0814b9e292c
105+
* @testdox decode a sentence
106+
*/
107+
public function testDecodeASentence(): void
108+
{
109+
$this->assertEquals(
110+
'anobstacleisoftenasteppingstone',
111+
decode('zmlyh gzxov rhlug vmzhg vkkrm thglm v')
112+
);
113+
}
114+
115+
/**
116+
* uuid 18729de3-de74-49b8-b68c-025eaf77f851
117+
* @testdox decode numbers
118+
*/
119+
public function testDecodeNumbers(): void
120+
{
121+
$this->assertEquals(
122+
"testing123testing",
123+
decode('gvhgr mt123 gvhgr mt')
124+
);
125+
}
126+
127+
/**
128+
* uuid 0f30325f-f53b-415d-ad3e-a7a4f63de034
129+
* @testdox decode all the letters
130+
*/
131+
public function testDecodeAllTheLetters(): void
132+
{
133+
$this->assertEquals(
134+
"thequickbrownfoxjumpsoverthelazydog",
135+
decode('gsvjf rxpyi ldmul cqfnk hlevi gsvoz abwlt')
136+
);
137+
}
138+
139+
/**
140+
* uuid 39640287-30c6-4c8c-9bac-9d613d1a5674
141+
* @testdox decode with too many spaces
142+
*/
143+
public function testDecodeWithTooManySpaces(): void
144+
{
145+
$this->assertEquals(
146+
'exercism',
147+
decode('vc vix r hn')
148+
);
149+
}
150+
151+
/**
152+
* uuid b34edf13-34c0-49b5-aa21-0768928000d5
153+
* @testdox decode with no spaces
154+
*/
155+
public function testDecodeWithNoSpacesInInput(): void
156+
{
157+
$this->assertEquals(
158+
'anobstacleisoftenasteppingstone',
159+
decode('zmlyhgzxovrhlugvmzhgvkkrmthglmv')
160+
);
161+
}
75162
}

0 commit comments

Comments
 (0)