Skip to content

Commit 06f8456

Browse files
authored
Sync crypto-square (#866)
1 parent 0af034f commit 06f8456

File tree

4 files changed

+66
-65
lines changed

4 files changed

+66
-65
lines changed

exercises/practice/crypto-square/.docs/instructions.md

Lines changed: 16 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ Implement the classic method for composing secret messages called a square code.
44

55
Given an English text, output the encoded version of that text.
66

7-
First, the input is normalized: the spaces and punctuation are removed
8-
from the English text and the message is downcased.
7+
First, the input is normalized: the spaces and punctuation are removed from the English text and the message is down-cased.
98

10-
Then, the normalized characters are broken into rows. These rows can be
11-
regarded as forming a rectangle when printed with intervening newlines.
9+
Then, the normalized characters are broken into rows.
10+
These rows can be regarded as forming a rectangle when printed with intervening newlines.
1211

1312
For example, the sentence
1413

@@ -22,13 +21,16 @@ is normalized to:
2221
"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"
2322
```
2423

25-
The plaintext should be organized in to a rectangle. The size of the
26-
rectangle (`r x c`) should be decided by the length of the message,
27-
such that `c >= r` and `c - r <= 1`, where `c` is the number of columns
28-
and `r` is the number of rows.
24+
The plaintext should be organized into a rectangle as square as possible.
25+
The size of the rectangle should be decided by the length of the message.
2926

30-
Our normalized text is 54 characters long, dictating a rectangle with
31-
`c = 8` and `r = 7`:
27+
If `c` is the number of columns and `r` is the number of rows, then for the rectangle `r` x `c` find the smallest possible integer `c` such that:
28+
29+
- `r * c >= length of message`,
30+
- and `c >= r`,
31+
- and `c - r <= 1`.
32+
33+
Our normalized text is 54 characters long, dictating a rectangle with `c = 8` and `r = 7`:
3234

3335
```text
3436
"ifmanwas"
@@ -40,26 +42,22 @@ Our normalized text is 54 characters long, dictating a rectangle with
4042
"sroots "
4143
```
4244

43-
The coded message is obtained by reading down the columns going left to
44-
right.
45+
The coded message is obtained by reading down the columns going left to right.
4546

4647
The message above is coded as:
4748

4849
```text
4950
"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau"
5051
```
5152

52-
Output the encoded text in chunks that fill perfect rectangles `(r X c)`,
53-
with `c` chunks of `r` length, separated by spaces. For phrases that are
54-
`n` characters short of the perfect rectangle, pad each of the last `n`
55-
chunks with a single trailing space.
53+
Output the encoded text in chunks that fill perfect rectangles `(r X c)`, with `c` chunks of `r` length, separated by spaces.
54+
For phrases that are `n` characters short of the perfect rectangle, pad each of the last `n` chunks with a single trailing space.
5655

5756
```text
5857
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau "
5958
```
6059

61-
Notice that were we to stack these, we could visually decode the
62-
ciphertext back in to the original message:
60+
Notice that were we to stack these, we could visually decode the ciphertext back in to the original message:
6361

6462
```text
6563
"imtgdvs"

exercises/practice/crypto-square/.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 crypto_square($plaintext)

exercises/practice/crypto-square/.meta/tests.toml

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,20 @@
1-
# This is an auto-generated file. Regular comments will be removed when this
2-
# file is regenerated. Regenerating will not touch any manually added keys,
3-
# so comments can be added in a "comment" key.
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
411

512
[407c3837-9aa7-4111-ab63-ec54b58e8e9f]
613
description = "empty plaintext results in an empty ciphertext"
714

15+
[aad04a25-b8bb-4304-888b-581bea8e0040]
16+
description = "normalization results in empty plaintext"
17+
818
[64131d65-6fd9-4f58-bdd8-4a2370fb481d]
919
description = "Lowercase"
1020

exercises/practice/crypto-square/CryptoSquareTest.php

Lines changed: 37 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
class CryptoSquareTest extends PHPUnit\Framework\TestCase
@@ -31,36 +9,73 @@ public static function setUpBeforeClass(): void
319
require_once 'CryptoSquare.php';
3210
}
3311

12+
/**
13+
* uuid 407c3837-9aa7-4111-ab63-ec54b58e8e9f
14+
* @testdox empty plaintext results in an empty ciphertext
15+
*/
3416
public function testEmptyPlaintextResultsInAnEmptyCiphertext(): void
3517
{
3618
$this->assertEquals("", crypto_square(""));
3719
}
3820

21+
/**
22+
* uuid aad04a25-b8bb-4304-888b-581bea8e0040
23+
* @testdox normalization results in empty plaintext
24+
*/
25+
public function testNormalizationResultsInEmptyPlaintext(): void
26+
{
27+
$this->assertEquals("", crypto_square("... --- ..."));
28+
}
29+
30+
/**
31+
* uuid 64131d65-6fd9-4f58-bdd8-4a2370fb481d
32+
* @testdox Lowercase
33+
*/
3934
public function testLowercase(): void
4035
{
4136
$this->assertEquals("a", crypto_square("A"));
4237
}
4338

39+
/**
40+
* uuid 63a4b0ed-1e3c-41ea-a999-f6f26ba447d6
41+
* @testdox Remove spaces
42+
*/
4443
public function testRemoveSpaces(): void
4544
{
4645
$this->assertEquals("b", crypto_square(" b "));
4746
}
4847

48+
/**
49+
* uuid 1b5348a1-7893-44c1-8197-42d48d18756c
50+
* @testdox Remove punctuation
51+
*/
4952
public function testRemovePunctuation(): void
5053
{
5154
$this->assertEquals("1", crypto_square("@1,%!"));
5255
}
5356

57+
/**
58+
* uuid 8574a1d3-4a08-4cec-a7c7-de93a164f41a
59+
* @testdox 9 character plaintext results in 3 chunks of 3 characters
60+
*/
5461
public function test9CharacterPlaintextResultsIn3ChunksOf3Characters(): void
5562
{
5663
$this->assertEquals("tsf hiu isn", crypto_square("This is fun!"));
5764
}
5865

66+
/**
67+
* uuid a65d3fa1-9e09-43f9-bcec-7a672aec3eae
68+
* @testdox 8 character plaintext results in 3 chunks, the last one with a trailing space
69+
*/
5970
public function test8CharacterPlaintextResultsIn3ChunksTheLastOneWithATrailingSpace(): void
6071
{
6172
$this->assertEquals("clu hlt io ", crypto_square("Chill out."));
6273
}
6374

75+
/**
76+
* uuid fbcb0c6d-4c39-4a31-83f6-c473baa6af80
77+
* @testdox 54 character plaintext results in 7 chunks, the last two with trailing spaces
78+
*/
6479
public function test54CharacterPlaintextResultsIn7ChunksTheLastTwoWithTrailingSpaces(): void
6580
{
6681
$this->assertEquals(

0 commit comments

Comments
 (0)