Skip to content

Commit e8835c9

Browse files
authored
Add diamond (#339)
1 parent 71f99f2 commit e8835c9

File tree

7 files changed

+226
-0
lines changed

7 files changed

+226
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,14 @@
651651
"prerequisites": [],
652652
"difficulty": 4
653653
},
654+
{
655+
"slug": "diamond",
656+
"name": "Diamond",
657+
"uuid": "79acd8ec-adf4-4163-8361-0f71950213fb",
658+
"practices": [],
659+
"prerequisites": [],
660+
"difficulty": 5
661+
},
654662
{
655663
"slug": "grade-school",
656664
"name": "Grade School",
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"diamond.vim"
8+
],
9+
"test": [
10+
"diamond.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
function! Rows(letter) abort
2+
if a:letter ==# 'A'
3+
return 'A'
4+
endif
5+
6+
let l:start = char2nr('A')
7+
let l:end = char2nr(a:letter)
8+
let l:size = l:end - l:start + 1
9+
10+
let l:diamond = []
11+
for l:i in range(0, l:size - 1)
12+
let l:outer = repeat(' ', l:size - 1 - i)
13+
let l:inner = nr2char(l:start + i)
14+
if l:i > 0
15+
let l:inner = l:inner . repeat(' ', 2 * l:i - 1) . l:inner
16+
endif
17+
let line = l:outer . l:inner . l:outer
18+
call add(l:diamond, l:line)
19+
endfor
20+
21+
for l:i in range(l:size - 2, 0, -1)
22+
call add(l:diamond, l:diamond[l:i])
23+
endfor
24+
25+
return join(l:diamond, '\n')
26+
endfunction
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"
+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
Execute (Degenerate case with a single 'A' row):
2+
let g:letter = 'A'
3+
let g:expected = 'A'
4+
AssertEqual g:expected, Rows(g:letter)
5+
6+
Execute (Degenerate case with no row containing 3 distinct groups of spaces):
7+
let g:letter = 'B'
8+
let g:expected = join([
9+
\ ' A ',
10+
\ 'B B',
11+
\ ' A '], '\n')
12+
AssertEqual g:expected, Rows(g:letter)
13+
14+
Execute (Smallest non-degenerate case with odd diamond side length):
15+
let g:letter = 'C'
16+
let g:expected = join([
17+
\ ' A ',
18+
\ ' B B ',
19+
\ 'C C',
20+
\ ' B B ',
21+
\ ' A '], '\n')
22+
AssertEqual g:expected, Rows(g:letter)
23+
24+
Execute (Smallest non-degenerate case with even diamond side length):
25+
let g:letter = 'D'
26+
let g:expected = join([
27+
\ ' A ',
28+
\ ' B B ',
29+
\ ' C C ',
30+
\ 'D D',
31+
\ ' C C ',
32+
\ ' B B ',
33+
\ ' A '], '\n')
34+
AssertEqual g:expected, Rows(g:letter)
35+
36+
Execute (Largest possible diamond):
37+
let g:letter = 'Z'
38+
let g:expected = join([
39+
\ ' A ',
40+
\ ' B B ',
41+
\ ' C C ',
42+
\ ' D D ',
43+
\ ' E E ',
44+
\ ' F F ',
45+
\ ' G G ',
46+
\ ' H H ',
47+
\ ' I I ',
48+
\ ' J J ',
49+
\ ' K K ',
50+
\ ' L L ',
51+
\ ' M M ',
52+
\ ' N N ',
53+
\ ' O O ',
54+
\ ' P P ',
55+
\ ' Q Q ',
56+
\ ' R R ',
57+
\ ' S S ',
58+
\ ' T T ',
59+
\ ' U U ',
60+
\ ' V V ',
61+
\ ' W W ',
62+
\ ' X X ',
63+
\ ' Y Y ',
64+
\ 'Z Z',
65+
\ ' Y Y ',
66+
\ ' X X ',
67+
\ ' W W ',
68+
\ ' V V ',
69+
\ ' U U ',
70+
\ ' T T ',
71+
\ ' S S ',
72+
\ ' R R ',
73+
\ ' Q Q ',
74+
\ ' P P ',
75+
\ ' O O ',
76+
\ ' N N ',
77+
\ ' M M ',
78+
\ ' L L ',
79+
\ ' K K ',
80+
\ ' J J ',
81+
\ ' I I ',
82+
\ ' H H ',
83+
\ ' G G ',
84+
\ ' F F ',
85+
\ ' E E ',
86+
\ ' D D ',
87+
\ ' C C ',
88+
\ ' B B ',
89+
\ ' A '], '\n')
90+
AssertEqual g:expected, Rows(g:letter)
+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
"
2+
" Generate a formatted diamond based on an input letter.
3+
"
4+
function! Rows(letter) abort
5+
" your implementation goes here
6+
endfunction

0 commit comments

Comments
 (0)