Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,14 @@
"prerequisites": [],
"difficulty": 8
},
{
"slug": "diamond",
"name": "Diamond",
"uuid": "8f223ea0-4bd2-4e40-99ac-d5d13d9726e5",
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "hamming",
"name": "Hamming",
Expand Down
52 changes: 52 additions & 0 deletions exercises/practice/diamond/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Instructions

The diamond kata takes as its input a letter, and outputs it in a diamond shape.
Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.

## Requirements

- The first row contains one 'A'.
- The last row contains one 'A'.
- All rows, except the first and last, have exactly two identical letters.
- All rows have as many trailing spaces as leading spaces. (This might be 0).
- The diamond is horizontally symmetric.
- The diamond is vertically symmetric.
- The diamond has a square shape (width equals height).
- The letters form a diamond shape.
- The top half has the letters in ascending order.
- The bottom half has the letters in descending order.
- The four corners (containing the spaces) are triangles.

## Examples

In the following examples, spaces are indicated by `·` characters.

Diamond for letter 'A':

```text
A
```

Diamond for letter 'C':

```text
··A··
·B·B·
C···C
·B·B·
··A··
```

Diamond for letter 'E':

```text
····A····
···B·B···
··C···C··
·D·····D·
E·······E
·D·····D·
··C···C··
···B·B···
····A····
```
19 changes: 19 additions & 0 deletions exercises/practice/diamond/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"jimmytty"
],
"files": {
"solution": [
"diamond.sql"
],
"test": [
"diamond_test.sql"
],
"example": [
".meta/example.sql"
]
},
"blurb": "Given a letter, print a diamond starting with 'A' with the supplied letter at the widest point.",
"source": "Seb Rose",
"source_url": "https://web.archive.org/web/20220807163751/http://claysnow.co.uk/recycling-tests-in-tdd/"
}
37 changes: 37 additions & 0 deletions exercises/practice/diamond/.meta/example.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
UPDATE diamond
SET result = (
WITH
lengths (len, chr) AS (
SELECT (ROW_NUMBER() OVER () - 1) * 2,
CHAR(value)
FROM GENERATE_SERIES(UNICODE('A'), UNICODE(letter))
),
lines (line) AS (
SELECT IIF(chr = 'A', chr, PRINTF('%-*c%c', len, chr, chr))
FROM lengths
),
padding (rn, line) AS (
SELECT ROW_NUMBER() OVER (),
PRINTF('%*s%s%-*s', pad, '', line, pad, '')
FROM (
SELECT line,
(SELECT MAX(LENGTH(line)) FROM lines) len,
((SELECT MAX(LENGTH(line)) FROM lines) - LENGTH(line))
/ 2 pad
FROM lines
)
),
top (line) AS (SELECT line FROM padding ORDER BY rn ASC),
bottom (line) AS (
SELECT line
FROM padding
WHERE rn < (SELECT MAX(rn) FROM padding) ORDER BY rn DESC
)
SELECT GROUP_CONCAT(line, CHAR(10))
FROM (
SELECT line FROM top
UNION ALL
SELECT line FROM bottom
)
)
;
25 changes: 25 additions & 0 deletions exercises/practice/diamond/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# 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.

[202fb4cc-6a38-4883-9193-a29d5cb92076]
description = "Degenerate case with a single 'A' row"

[bd6a6d78-9302-42e9-8f60-ac1461e9abae]
description = "Degenerate case with no row containing 3 distinct groups of spaces"

[af8efb49-14ed-447f-8944-4cc59ce3fd76]
description = "Smallest non-degenerate case with odd diamond side length"

[e0c19a95-9888-4d05-86a0-fa81b9e70d1d]
description = "Smallest non-degenerate case with even diamond side length"

[82ea9aa9-4c0e-442a-b07e-40204e925944]
description = "Largest possible diamond"
10 changes: 10 additions & 0 deletions exercises/practice/diamond/create_fixture.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
DROP TABLE IF EXISTS diamond;
CREATE TABLE diamond (
letter TEXT NOT NULL,
result TEXT
);

.mode csv
.import ./data.csv diamond

UPDATE diamond SET result = NULL;
25 changes: 25 additions & 0 deletions exercises/practice/diamond/create_test_table.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
DROP TABLE IF EXISTS tests;
CREATE TABLE IF NOT EXISTS tests (
-- uuid and description are taken from the test.toml file
uuid TEXT PRIMARY KEY,
description TEXT NOT NULL,
-- The following section is needed by the online test-runner
status TEXT DEFAULT 'fail',
message TEXT,
output TEXT,
test_code TEXT,
task_id INTEGER DEFAULT NULL,
-- Here are columns for the actual tests
letter TEXT NOT NULL,
expected TEXT NOT NULL
);

INSERT INTO tests (uuid, description, letter, expected)
VALUES
('202fb4cc-6a38-4883-9193-a29d5cb92076', 'Degenerate case with a single ''A'' row', 'A', 'A'),
('bd6a6d78-9302-42e9-8f60-ac1461e9abae', 'Degenerate case with no row containing 3 distinct groups of spaces', 'B', ' A \nB B\n A '),
('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 '),
('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 '),
('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 ');

UPDATE tests SET expected = REPLACE(expected, '\n', CHAR(10));
5 changes: 5 additions & 0 deletions exercises/practice/diamond/data.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"A",
"B",
"C",
"D",
"Z",
7 changes: 7 additions & 0 deletions exercises/practice/diamond/diamond.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Schema:
-- CREATE TABLE diamond (
-- letter TEXT NOT NULL,
-- result TEXT
-- );
--
-- Task: update the diamond table and set result column based on the letter.
36 changes: 36 additions & 0 deletions exercises/practice/diamond/diamond_test.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-- Create database:
.read ./create_fixture.sql

-- Read user student solution and save any output as markdown in user_output.md:
.mode markdown
.output user_output.md
.read ./diamond.sql
.output

-- Create a clean testing environment:
.read ./create_test_table.sql

-- Comparison of user input and the tests updates the status for each test:
UPDATE tests
SET status = 'pass'
FROM (SELECT letter, result FROM diamond) AS actual
WHERE (actual.letter, actual.result) = (tests.letter, tests.expected);

-- Update message for failed tests to give helpful information:
UPDATE tests
SET message = (
'Result for "' || tests.letter || '"' || ' is <'
|| COALESCE(actual.result, 'NULL')
|| '> but should be <' || tests.expected || '>'
)
FROM (SELECT letter, result FROM diamond) AS actual
WHERE actual.letter = tests.letter AND tests.status = 'fail';

-- Save results to ./output.json (needed by the online test-runner)
.mode json
.once './output.json'
SELECT description, status, message, output, test_code, task_id FROM tests;

-- Display test results in readable form for the student:
.mode table
SELECT description, status, message FROM tests;