Skip to content

Commit 7756170

Browse files
committed
* add practice exercise: diamond
1 parent 84f4acd commit 7756170

File tree

11 files changed

+866
-0
lines changed

11 files changed

+866
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,14 @@
430430
"practices": [],
431431
"prerequisites": [],
432432
"difficulty": 3
433+
},
434+
{
435+
"slug": "diamond",
436+
"name": "Diamond",
437+
"uuid": "43a0f86b-25a4-4993-a906-7fdf8a179a7f",
438+
"practices": [],
439+
"prerequisites": [],
440+
"difficulty": 3
433441
}
434442
]
435443
},
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.pas"
8+
],
9+
"test": [
10+
"TestCases.pas"
11+
],
12+
"example": [
13+
".meta/example.pas"
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: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
unit Diamond;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function rows(const letter : char) : string;
8+
9+
implementation
10+
11+
uses SysUtils, StrUtils, Classes;
12+
13+
function rows(const letter : char) : string;
14+
var
15+
i : integer;
16+
InterSpaces : integer;
17+
PadSpaces : integer;
18+
Diamond : TStringList;
19+
begin
20+
Diamond := TStringList.Create;
21+
Diamond.TextLineBreakStyle := tlbsLF;
22+
Diamond.SkipLastLineBreak := true;
23+
PadSpaces := (ord(letter) - ord('A')) * 2 + 1;
24+
Diamond.Add(PadCenter('A', PadSpaces));
25+
for i := ord('B') to ord(letter) do
26+
begin
27+
InterSpaces := (i - ord('A')) * 2 - 1;
28+
Diamond.Add(
29+
PadCenter(
30+
char(i) + StringOfChar(char(32), InterSpaces) + char(i),
31+
PadSpaces
32+
)
33+
);
34+
end;
35+
for i := Diamond.Count - 2 downto 0 do Diamond.Add(Diamond[i]);
36+
result := Diamond.Text;
37+
Diamond.Free;
38+
end;
39+
40+
end.
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: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
unit Diamond;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
function rows(const letter : char) : string;
8+
9+
implementation
10+
11+
uses SysUtils;
12+
13+
function rows(const letter : char) : string;
14+
begin
15+
16+
raise ENotImplemented.Create('Please implement your solution.'); result := format('"··%s··"', [letter]);
17+
18+
end;
19+
20+
end.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
SHELL = /bin/bash
2+
MAKEFLAGS += --no-print-directory
3+
DESTDIR = build
4+
EXECUTABLE = $(DESTDIR)/test
5+
COMMAND = fpc -l- -v0 -g -gl -Sehnw -Fu./lib test.pas -FE"./$(DESTDIR)"
6+
7+
.ONESHELL:
8+
9+
test:
10+
@mkdir -p "./$(DESTDIR)"
11+
@cp -r ./lib "./$(DESTDIR)"
12+
@$(COMMAND) && ./$(EXECUTABLE) $(test)
13+
14+
clean:
15+
@rm -fr "./$(DESTDIR)"
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
unit TestCases;
2+
3+
{$mode ObjFPC}{$H+}
4+
5+
interface
6+
7+
uses Classes, SysUtils, FPCUnit, TestRegistry, FPCUnitTestUtils;
8+
9+
type
10+
DiamondTest = class(TTestCase)
11+
published
12+
procedure degenerate_case_with_a_single_a_row;
13+
procedure degenerate_case_with_no_row_containing_3_distinct_groups_of_spaces;
14+
procedure smallest_non_degenerate_case_with_odd_diamond_side_length;
15+
procedure smallest_non_degenerate_case_with_even_diamond_side_length;
16+
procedure largest_possible_diamond;
17+
end;
18+
19+
implementation
20+
21+
uses Diamond;
22+
23+
// 202fb4cc-6a38-4883-9193-a29d5cb92076
24+
procedure DiamondTest.degenerate_case_with_a_single_a_row;
25+
begin
26+
TapAssertTrue(Self, 'Degenerate case with a single ''A'' row', 'A', Diamond.rows('A'));
27+
end;
28+
29+
// bd6a6d78-9302-42e9-8f60-ac1461e9abae
30+
procedure DiamondTest.degenerate_case_with_no_row_containing_3_distinct_groups_of_spaces;
31+
begin
32+
TapAssertTrue(Self, 'Degenerate case with no row containing 3 distinct groups of spaces', ' A ' + #10 + 'B B' + #10 + ' A ', Diamond.rows('B'));
33+
end;
34+
35+
// af8efb49-14ed-447f-8944-4cc59ce3fd76
36+
procedure DiamondTest.smallest_non_degenerate_case_with_odd_diamond_side_length;
37+
begin
38+
TapAssertTrue(Self, 'Smallest non-degenerate case with odd diamond side length', ' A ' + #10 + ' B B ' + #10 + 'C C' + #10 + ' B B ' + #10 + ' A ', Diamond.rows('C'));
39+
end;
40+
41+
// e0c19a95-9888-4d05-86a0-fa81b9e70d1d
42+
procedure DiamondTest.smallest_non_degenerate_case_with_even_diamond_side_length;
43+
begin
44+
TapAssertTrue(Self, 'Smallest non-degenerate case with even diamond side length', ' A ' + #10 + ' B B ' + #10 + ' C C ' + #10 + 'D D' + #10 + ' C C ' + #10 + ' B B ' + #10 + ' A ', Diamond.rows('D'));
45+
end;
46+
47+
// 82ea9aa9-4c0e-442a-b07e-40204e925944
48+
procedure DiamondTest.largest_possible_diamond;
49+
begin
50+
TapAssertTrue(Self, 'Largest possible diamond', ' A ' + #10 + ' B B ' + #10 + ' C C ' + #10 + ' D D ' + #10 + ' E E ' + #10 + ' F F ' + #10 + ' G G ' + #10 + ' H H ' + #10 + ' I I ' + #10 + ' J J ' + #10 + ' K K ' + #10 + ' L L ' + #10 + ' M M ' + #10 + ' N N ' + #10 + ' O O ' + #10 + ' P P ' + #10 + ' Q Q ' + #10 + ' R R ' + #10 + ' S S ' + #10 + ' T T ' + #10 + ' U U ' + #10 + ' V V ' + #10 + ' W W ' + #10 + ' X X ' + #10 + ' Y Y ' + #10 + 'Z Z' + #10 + ' Y Y ' + #10 + ' X X ' + #10 + ' W W ' + #10 + ' V V ' + #10 + ' U U ' + #10 + ' T T ' + #10 + ' S S ' + #10 + ' R R ' + #10 + ' Q Q ' + #10 + ' P P ' + #10 + ' O O ' + #10 + ' N N ' + #10 + ' M M ' + #10 + ' L L ' + #10 + ' K K ' + #10 + ' J J ' + #10 + ' I I ' + #10 + ' H H ' + #10 + ' G G ' + #10 + ' F F ' + #10 + ' E E ' + #10 + ' D D ' + #10 + ' C C ' + #10 + ' B B ' + #10 + ' A ', Diamond.rows('Z'));
51+
end;
52+
53+
initialization
54+
RegisterTest(DiamondTest);
55+
56+
end.

0 commit comments

Comments
 (0)