Skip to content

Commit db189c1

Browse files
authored
add practice exercise Diamond (#766)
* add practice exercise diamond * fixed example.jl glitch
1 parent f666df6 commit db189c1

File tree

7 files changed

+212
-0
lines changed

7 files changed

+212
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -994,6 +994,14 @@
994994
"prerequisites": [],
995995
"difficulty": 3
996996
},
997+
{
998+
"slug": "diamond",
999+
"name": "Diamond",
1000+
"uuid": "e082b59e-5c2a-4474-8c73-ceb623bcd858",
1001+
"practices": [],
1002+
"prerequisites": [],
1003+
"difficulty": 3
1004+
},
9971005
{
9981006
"slug": "matrix",
9991007
"name": "Matrix",
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+
"colinleach"
4+
],
5+
"files": {
6+
"solution": [
7+
"diamond.jl"
8+
],
9+
"test": [
10+
"runtests.jl"
11+
],
12+
"example": [
13+
".meta/example.jl"
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: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
function rows(letter)
2+
letter == "A" && return ["A"]
3+
4+
letters = 'A':'Z' |> collect
5+
letter_index = findfirst(x -> x == only(letter), letters)
6+
rowslist = []
7+
8+
# loop to the middle of the diamond
9+
for i in 1:letter_index
10+
row = [' ' for _ in 1:(2 * letter_index - 1)]
11+
row[letter_index - i + 1] = row[letter_index + i - 1] = letters[i]
12+
push!(rowslist, join(row))
13+
end
14+
15+
# reflect most of the top half and concatenate
16+
vcat(rowslist, rowslist[(end - 1):-1:1])
17+
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"

exercises/practice/diamond/diamond.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
function rows(letter)
2+
3+
end
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
using Test
2+
3+
include("diamond.jl")
4+
5+
@testset verbose = true "tests" begin
6+
@testset "Degenerate case with a single 'A' row" begin
7+
@test rows("A") == ["A"]
8+
end
9+
10+
@testset "Degenerate case with no row containing 3 distinct groups of spaces" begin
11+
@test rows("B") == [" A ", "B B", " A "]
12+
end
13+
14+
@testset "Smallest non-degenerate case with odd diamond side length" begin
15+
result = [" A ", " B B ", "C C", " B B ", " A "]
16+
@test rows("C") == result
17+
end
18+
19+
@testset "Smallest non-degenerate case with even diamond side length" begin
20+
result = [
21+
" A ",
22+
" B B ",
23+
" C C ",
24+
"D D",
25+
" C C ",
26+
" B B ",
27+
" A ",
28+
]
29+
@test rows("D") == result
30+
end
31+
32+
@testset "Largest possible diamond" begin
33+
result = [
34+
" A ",
35+
" B B ",
36+
" C C ",
37+
" D D ",
38+
" E E ",
39+
" F F ",
40+
" G G ",
41+
" H H ",
42+
" I I ",
43+
" J J ",
44+
" K K ",
45+
" L L ",
46+
" M M ",
47+
" N N ",
48+
" O O ",
49+
" P P ",
50+
" Q Q ",
51+
" R R ",
52+
" S S ",
53+
" T T ",
54+
" U U ",
55+
" V V ",
56+
" W W ",
57+
" X X ",
58+
" Y Y ",
59+
"Z Z",
60+
" Y Y ",
61+
" X X ",
62+
" W W ",
63+
" V V ",
64+
" U U ",
65+
" T T ",
66+
" S S ",
67+
" R R ",
68+
" Q Q ",
69+
" P P ",
70+
" O O ",
71+
" N N ",
72+
" M M ",
73+
" L L ",
74+
" K K ",
75+
" J J ",
76+
" I I ",
77+
" H H ",
78+
" G G ",
79+
" F F ",
80+
" E E ",
81+
" D D ",
82+
" C C ",
83+
" B B ",
84+
" A ",
85+
]
86+
@test rows("Z") == result
87+
end
88+
end

0 commit comments

Comments
 (0)