-
-
Notifications
You must be signed in to change notification settings - Fork 142
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10f0cfe
commit 48c768a
Showing
7 changed files
with
269 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
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": [ | ||
"tomasnorre" | ||
], | ||
"files": { | ||
"solution": [ | ||
"RotationalCipher.php" | ||
], | ||
"test": [ | ||
"RotationalCipherTest.php" | ||
], | ||
"example": [ | ||
".meta/example.php" | ||
] | ||
}, | ||
"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,22 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class RotationalCipher | ||
{ | ||
public function rotate(string $text, int $shift): string | ||
{ | ||
return implode('', array_map(static function ($c) use ($shift) { | ||
$isUpper = preg_match('/[A-Z]/', $c); | ||
$isAlpha = preg_match('/[a-z]/i', $c); | ||
$charShift = $isUpper ? ord('A') : ord('a'); | ||
|
||
if ($isAlpha) { | ||
$shiftedCharCode = (ord($c) - $charShift + $shift) % 26 + $charShift; | ||
return chr($shiftedCharCode); | ||
} | ||
|
||
return $c; | ||
}, str_split($text))); | ||
} | ||
} |
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,33 @@ | ||
<?php | ||
|
||
/* | ||
* By adding type hints and enabling strict type checking, code can become | ||
* easier to read, self-documenting and reduce the number of potential bugs. | ||
* By default, type declarations are non-strict, which means they will attempt | ||
* to change the original type to match the type specified by the | ||
* type-declaration. | ||
* | ||
* In other words, if you pass a string to a function requiring a float, | ||
* it will attempt to convert the string value to a float. | ||
* | ||
* To enable strict mode, a single declare directive must be placed at the top | ||
* of the file. | ||
* This means that the strictness of typing is configured on a per-file basis. | ||
* This directive not only affects the type declarations of parameters, but also | ||
* a function's return type. | ||
* | ||
* For more info review the Concept on strict type checking in the PHP track | ||
* <link>. | ||
* | ||
* To disable strict typing, comment out the directive below. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
class RotationalCipher | ||
{ | ||
public function rotate(string $text, int $shift): string | ||
{ | ||
throw new \BadMethodCallException(sprintf('Implement the %s method', __FUNCTION__)); | ||
} | ||
} |
118 changes: 118 additions & 0 deletions
118
exercises/practice/rotational-cipher/RotationalCipherTest.php
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,118 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
class RotationalCipherTest extends PHPUnit\Framework\TestCase | ||
{ | ||
private RotationalCipher $rotationalCipher; | ||
|
||
public static function setUpBeforeClass(): void | ||
{ | ||
require_once 'RotationalCipher.php'; | ||
} | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->rotationalCipher = new RotationalCipher(); | ||
} | ||
|
||
/** | ||
* uuid: 74e58a38-e484-43f1-9466-877a7515e10f | ||
*/ | ||
public function testRotateAByZero(): void | ||
{ | ||
$expected = 'a'; | ||
$actual = $this->rotationalCipher->rotate('a', 0); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: 7ee352c6-e6b0-4930-b903-d09943ecb8f5 | ||
*/ | ||
public function testRotateAByOne(): void | ||
{ | ||
$expected = 'b'; | ||
$actual = $this->rotationalCipher->rotate('a', 1); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: edf0a733-4231-4594-a5ee-46a4009ad764 | ||
*/ | ||
public function testRotateABy26(): void | ||
{ | ||
$expected = 'a'; | ||
$actual = $this->rotationalCipher->rotate('a', 26); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: e3e82cb9-2a5b-403f-9931-e43213879300 | ||
*/ | ||
public function testRotateMBy13(): void | ||
{ | ||
$expected = 'z'; | ||
$actual = $this->rotationalCipher->rotate('m', 13); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: 19f9eb78-e2ad-4da4-8fe3-9291d47c1709 | ||
*/ | ||
public function testRotateNBy13WithWrapAroundAlphabet(): void | ||
{ | ||
$expected = 'a'; | ||
$actual = $this->rotationalCipher->rotate('n', 13); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: a116aef4-225b-4da9-884f-e8023ca6408a | ||
*/ | ||
public function testRotateCapitalLetters(): void | ||
{ | ||
$expected = 'TRL'; | ||
$actual = $this->rotationalCipher->rotate('OMG', 5); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: 71b541bb-819c-4dc6-a9c3-132ef9bb737b | ||
*/ | ||
public function testRotateSpaces(): void | ||
{ | ||
$expected = 'T R L'; | ||
$actual = $this->rotationalCipher->rotate('O M G', 5); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: ef32601d-e9ef-4b29-b2b5-8971392282e6 | ||
*/ | ||
public function testRotateNumbers(): void | ||
{ | ||
$expected = 'Xiwxmrk 1 2 3 xiwxmrk'; | ||
$actual = $this->rotationalCipher->rotate('Testing 1 2 3 testing', 4); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: 32dd74f6-db2b-41a6-b02c-82eb4f93e549 | ||
*/ | ||
public function testRotatePunctuation(): void | ||
{ | ||
$expected = "Gzo'n zvo, Bmviyhv!"; | ||
$actual = $this->rotationalCipher->rotate("Let's eat, Grandma!", 21); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
|
||
/** | ||
* uuid: 9fb93fe6-42b0-46e6-9ec1-0bf0a062d8c9 | ||
*/ | ||
public function testRotateAllLetters(): void | ||
{ | ||
$expected = 'Gur dhvpx oebja sbk whzcf bire gur ynml qbt.'; | ||
$actual = $this->rotationalCipher->rotate('The quick brown fox jumps over the lazy dog.', 13); | ||
$this->assertEquals($expected, $actual); | ||
} | ||
} |