Skip to content

Commit 027dadb

Browse files
authored
Merge pull request #302 from exercism/issue/301
Crypto Square
2 parents e75f839 + 63ad86e commit 027dadb

File tree

8 files changed

+855
-0
lines changed

8 files changed

+855
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,14 @@
713713
"practices": [],
714714
"prerequisites": [],
715715
"difficulty": 5
716+
},
717+
{
718+
"slug": "crypto-square",
719+
"name": "Crypto Square",
720+
"uuid": "7f5b4617-4994-4254-9e5f-71e0f16a86cd",
721+
"practices": [],
722+
"prerequisites": [],
723+
"difficulty": 5
716724
}
717725
],
718726
"foregone": [
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Instructions
2+
3+
Implement the classic method for composing secret messages called a square code.
4+
5+
Given an English text, output the encoded version of that text.
6+
7+
First, the input is normalized: the spaces and punctuation are removed from the English text and the message is down-cased.
8+
9+
Then, the normalized characters are broken into rows.
10+
These rows can be regarded as forming a rectangle when printed with intervening newlines.
11+
12+
For example, the sentence
13+
14+
```text
15+
"If man was meant to stay on the ground, god would have given us roots."
16+
```
17+
18+
is normalized to:
19+
20+
```text
21+
"ifmanwasmeanttostayonthegroundgodwouldhavegivenusroots"
22+
```
23+
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.
26+
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`:
34+
35+
```text
36+
"ifmanwas"
37+
"meanttos"
38+
"tayonthe"
39+
"groundgo"
40+
"dwouldha"
41+
"vegivenu"
42+
"sroots "
43+
```
44+
45+
The coded message is obtained by reading down the columns going left to right.
46+
47+
The message above is coded as:
48+
49+
```text
50+
"imtgdvsfearwermayoogoanouuiontnnlvtwttddesaohghnsseoau"
51+
```
52+
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.
55+
56+
```text
57+
"imtgdvs fearwer mayoogo anouuio ntnnlvt wttddes aohghn sseoau "
58+
```
59+
60+
Notice that were we to stack these, we could visually decode the ciphertext back in to the original message:
61+
62+
```text
63+
"imtgdvs"
64+
"fearwer"
65+
"mayoogo"
66+
"anouuio"
67+
"ntnnlvt"
68+
"wttddes"
69+
"aohghn "
70+
"sseoau "
71+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"rabestro"
4+
],
5+
"files": {
6+
"solution": [
7+
"crypto-square.awk"
8+
],
9+
"test": [
10+
"test-crypto-square.bats"
11+
],
12+
"example": [
13+
".meta/example.awk"
14+
]
15+
},
16+
"blurb": "Implement the classic method for composing secret messages called a square code.",
17+
"source": "J Dalbey's Programming Practice problems",
18+
"source_url": "https://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html"
19+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
plainText = tolower(gensub(/[^[:alnum:]]/, "", "g"))
3+
4+
len = length(plainText)
5+
side = int(sqrt(len))
6+
cols = side * side < len ? side + 1 : side
7+
rows = side * cols < len ? side + 1 : side
8+
9+
NF=0
10+
for (i = 0; i < rows * cols; ++i) {
11+
symbol = i >= len ? " " : substr(plainText, i + 1, 1)
12+
chunk = 1 + i % cols
13+
$chunk = $chunk symbol
14+
}
15+
print
16+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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.
11+
12+
[407c3837-9aa7-4111-ab63-ec54b58e8e9f]
13+
description = "empty plaintext results in an empty ciphertext"
14+
15+
[aad04a25-b8bb-4304-888b-581bea8e0040]
16+
description = "normalization results in empty plaintext"
17+
18+
[64131d65-6fd9-4f58-bdd8-4a2370fb481d]
19+
description = "Lowercase"
20+
21+
[63a4b0ed-1e3c-41ea-a999-f6f26ba447d6]
22+
description = "Remove spaces"
23+
24+
[1b5348a1-7893-44c1-8197-42d48d18756c]
25+
description = "Remove punctuation"
26+
27+
[8574a1d3-4a08-4cec-a7c7-de93a164f41a]
28+
description = "9 character plaintext results in 3 chunks of 3 characters"
29+
30+
[a65d3fa1-9e09-43f9-bcec-7a672aec3eae]
31+
description = "8 character plaintext results in 3 chunks, the last one with a trailing space"
32+
33+
[fbcb0c6d-4c39-4a31-83f6-c473baa6af80]
34+
description = "54 character plaintext results in 7 chunks, the last two with trailing spaces"
35+
include = false
36+
37+
[33fd914e-fa44-445b-8f38-ff8fbc9fe6e6]
38+
description = "54 character plaintext results in 8 chunks, the last two with trailing spaces"
39+
reimplements = "fbcb0c6d-4c39-4a31-83f6-c473baa6af80"

0 commit comments

Comments
 (0)