Skip to content

Commit 76935bb

Browse files
authored
Add practice exercise: diamond (#197)
1 parent 4fd5965 commit 76935bb

File tree

10 files changed

+224
-0
lines changed

10 files changed

+224
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,14 @@
346346
"prerequisites": [],
347347
"difficulty": 8
348348
},
349+
{
350+
"slug": "diamond",
351+
"name": "Diamond",
352+
"uuid": "8f223ea0-4bd2-4e40-99ac-d5d13d9726e5",
353+
"practices": [],
354+
"prerequisites": [],
355+
"difficulty": 8
356+
},
349357
{
350358
"slug": "hamming",
351359
"name": "Hamming",
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
# Instructions
2+
3+
The diamond kata takes as its input a letter, and outputs it in a diamond shape.
4+
Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.
5+
6+
## Requirements
7+
8+
- The first row contains one 'A'.
9+
- The last row contains one 'A'.
10+
- All rows, except the first and last, have exactly two identical letters.
11+
- All rows have as many trailing spaces as leading spaces. (This might be 0).
12+
- The diamond is horizontally symmetric.
13+
- The diamond is vertically symmetric.
14+
- The diamond has a square shape (width equals height).
15+
- The letters form a diamond shape.
16+
- The top half has the letters in ascending order.
17+
- The bottom half has the letters in descending order.
18+
- The four corners (containing the spaces) are triangles.
19+
20+
## Examples
21+
22+
In the following examples, spaces are indicated by `·` characters.
23+
24+
Diamond for letter 'A':
25+
26+
```text
27+
A
28+
```
29+
30+
Diamond for letter 'C':
31+
32+
```text
33+
··A··
34+
·B·B·
35+
C···C
36+
·B·B·
37+
··A··
38+
```
39+
40+
Diamond for letter 'E':
41+
42+
```text
43+
····A····
44+
···B·B···
45+
··C···C··
46+
·D·····D·
47+
E·······E
48+
·D·····D·
49+
··C···C··
50+
···B·B···
51+
····A····
52+
```
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"jimmytty"
4+
],
5+
"files": {
6+
"solution": [
7+
"diamond.sql"
8+
],
9+
"test": [
10+
"diamond_test.sql"
11+
],
12+
"example": [
13+
".meta/example.sql"
14+
]
15+
},
16+
"blurb": "Given a letter, print a diamond starting with 'A' with the supplied letter at the widest point.",
17+
"source": "Seb Rose",
18+
"source_url": "https://web.archive.org/web/20220807163751/http://claysnow.co.uk/recycling-tests-in-tdd/"
19+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
UPDATE diamond
2+
SET result = (
3+
WITH
4+
lengths (len, chr) AS (
5+
SELECT (ROW_NUMBER() OVER () - 1) * 2,
6+
CHAR(value)
7+
FROM GENERATE_SERIES(UNICODE('A'), UNICODE(letter))
8+
),
9+
lines (line) AS (
10+
SELECT IIF(chr = 'A', chr, PRINTF('%-*c%c', len, chr, chr))
11+
FROM lengths
12+
),
13+
padding (rn, line) AS (
14+
SELECT ROW_NUMBER() OVER (),
15+
PRINTF('%*s%s%-*s', pad, '', line, pad, '')
16+
FROM (
17+
SELECT line,
18+
(SELECT MAX(LENGTH(line)) FROM lines) len,
19+
((SELECT MAX(LENGTH(line)) FROM lines) - LENGTH(line))
20+
/ 2 pad
21+
FROM lines
22+
)
23+
),
24+
top (line) AS (SELECT line FROM padding ORDER BY rn ASC),
25+
bottom (line) AS (
26+
SELECT line
27+
FROM padding
28+
WHERE rn < (SELECT MAX(rn) FROM padding) ORDER BY rn DESC
29+
)
30+
SELECT GROUP_CONCAT(line, CHAR(10))
31+
FROM (
32+
SELECT line FROM top
33+
UNION ALL
34+
SELECT line FROM bottom
35+
)
36+
)
37+
;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
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+
[202fb4cc-6a38-4883-9193-a29d5cb92076]
13+
description = "Degenerate case with a single 'A' row"
14+
15+
[bd6a6d78-9302-42e9-8f60-ac1461e9abae]
16+
description = "Degenerate case with no row containing 3 distinct groups of spaces"
17+
18+
[af8efb49-14ed-447f-8944-4cc59ce3fd76]
19+
description = "Smallest non-degenerate case with odd diamond side length"
20+
21+
[e0c19a95-9888-4d05-86a0-fa81b9e70d1d]
22+
description = "Smallest non-degenerate case with even diamond side length"
23+
24+
[82ea9aa9-4c0e-442a-b07e-40204e925944]
25+
description = "Largest possible diamond"
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
DROP TABLE IF EXISTS diamond;
2+
CREATE TABLE diamond (
3+
letter TEXT NOT NULL,
4+
result TEXT
5+
);
6+
7+
.mode csv
8+
.import ./data.csv diamond
9+
10+
UPDATE diamond SET result = NULL;
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
DROP TABLE IF EXISTS tests;
2+
CREATE TABLE IF NOT EXISTS tests (
3+
-- uuid and description are taken from the test.toml file
4+
uuid TEXT PRIMARY KEY,
5+
description TEXT NOT NULL,
6+
-- The following section is needed by the online test-runner
7+
status TEXT DEFAULT 'fail',
8+
message TEXT,
9+
output TEXT,
10+
test_code TEXT,
11+
task_id INTEGER DEFAULT NULL,
12+
-- Here are columns for the actual tests
13+
letter TEXT NOT NULL,
14+
expected TEXT NOT NULL
15+
);
16+
17+
INSERT INTO tests (uuid, description, letter, expected)
18+
VALUES
19+
('202fb4cc-6a38-4883-9193-a29d5cb92076', 'Degenerate case with a single ''A'' row', 'A', 'A'),
20+
('bd6a6d78-9302-42e9-8f60-ac1461e9abae', 'Degenerate case with no row containing 3 distinct groups of spaces', 'B', ' A \nB B\n A '),
21+
('af8efb49-14ed-447f-8944-4cc59ce3fd76', 'Smallest non-degenerate case with odd diamond side length', 'C', ' A \n B B \nC C\n B B \n A '),
22+
('e0c19a95-9888-4d05-86a0-fa81b9e70d1d', 'Smallest non-degenerate case with even diamond side length', 'D', ' A \n B B \n C C \nD D\n C C \n B B \n A '),
23+
('82ea9aa9-4c0e-442a-b07e-40204e925944', 'Largest possible diamond', 'Z', ' A \n B B \n C C \n D D \n E E \n F F \n G G \n H H \n I I \n J J \n K K \n L L \n M M \n N N \n O O \n P P \n Q Q \n R R \n S S \n T T \n U U \n V V \n W W \n X X \n Y Y \nZ Z\n Y Y \n X X \n W W \n V V \n U U \n T T \n S S \n R R \n Q Q \n P P \n O O \n N N \n M M \n L L \n K K \n J J \n I I \n H H \n G G \n F F \n E E \n D D \n C C \n B B \n A ');
24+
25+
UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10));

exercises/practice/diamond/data.csv

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
"A",
2+
"B",
3+
"C",
4+
"D",
5+
"Z",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
-- Schema:
2+
-- CREATE TABLE diamond (
3+
-- letter TEXT NOT NULL,
4+
-- result TEXT
5+
-- );
6+
--
7+
-- Task: update the diamond table and set result column based on the letter.
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
-- Create database:
2+
.read ./create_fixture.sql
3+
4+
-- Read user student solution and save any output as markdown in user_output.md:
5+
.mode markdown
6+
.output user_output.md
7+
.read ./diamond.sql
8+
.output
9+
10+
-- Create a clean testing environment:
11+
.read ./create_test_table.sql
12+
13+
-- Comparison of user input and the tests updates the status for each test:
14+
UPDATE tests
15+
SET status = 'pass'
16+
FROM (SELECT letter, result FROM diamond) AS actual
17+
WHERE (actual.letter, actual.result) = (tests.letter, tests.expected);
18+
19+
-- Update message for failed tests to give helpful information:
20+
UPDATE tests
21+
SET message = (
22+
'Result for "' || tests.letter || '"' || ' is <'
23+
|| COALESCE(actual.result, 'NULL')
24+
|| '> but should be <' || tests.expected || '>'
25+
)
26+
FROM (SELECT letter, result FROM diamond) AS actual
27+
WHERE actual.letter = tests.letter AND tests.status = 'fail';
28+
29+
-- Save results to ./output.json (needed by the online test-runner)
30+
.mode json
31+
.once './output.json'
32+
SELECT description, status, message, output, test_code, task_id FROM tests;
33+
34+
-- Display test results in readable form for the student:
35+
.mode table
36+
SELECT description, status, message FROM tests;

0 commit comments

Comments
 (0)