Skip to content

Commit 3391162

Browse files
authored
Add diamond (#371)
1 parent d5f9fa3 commit 3391162

File tree

7 files changed

+229
-0
lines changed

7 files changed

+229
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,14 @@
681681
"prerequisites": [],
682682
"difficulty": 5
683683
},
684+
{
685+
"slug": "diamond",
686+
"name": "Diamond",
687+
"uuid": "f1da1920-802f-4165-87a4-2381f530c262",
688+
"practices": [],
689+
"prerequisites": [],
690+
"difficulty": 5
691+
},
684692
{
685693
"slug": "house",
686694
"name": "House",
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.coffee"
8+
],
9+
"test": [
10+
"diamond.spec.coffee"
11+
],
12+
"example": [
13+
".meta/example.coffee"
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,27 @@
1+
class Diamond
2+
@rows: (letter) ->
3+
return 'A' if letter is 'A'
4+
5+
start = 'A'.charCodeAt 0
6+
end = letter.charCodeAt 0
7+
size = end - start + 1
8+
diamond = []
9+
10+
# Down to middle of diamond
11+
for i in [0...size]
12+
currentChar = String.fromCharCode start + i
13+
outer = ' '.repeat size - 1 - i
14+
if i is 0
15+
line = outer + currentChar + outer
16+
else
17+
inner = ' '.repeat 2 * i - 1
18+
line = outer + currentChar + inner + currentChar + outer
19+
diamond.push line
20+
21+
# flip the diamond to make the bottom half
22+
for i in [size-2..0]
23+
diamond.push diamond[i]
24+
25+
diamond.join '\n'
26+
27+
module.exports = Diamond
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"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
class Diamond
2+
@rows: (letter) ->
3+
4+
module.exports = Diamond
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
Diamond = require './diamond'
2+
3+
describe 'Diamond', ->
4+
it 'Degenerate case with a single A row', ->
5+
expected = [
6+
'A'
7+
].join '\n'
8+
expect(Diamond.rows 'A').toEqual expected
9+
10+
xit 'Degenerate case with no row containing 3 distinct groups of spaces', ->
11+
expected = [
12+
' A '
13+
'B B'
14+
' A '
15+
].join '\n'
16+
expect(Diamond.rows 'B').toEqual expected
17+
18+
xit 'Smallest non-degenerate case with odd diamond side length', ->
19+
expected = [
20+
' A '
21+
' B B '
22+
'C C'
23+
' B B '
24+
' A '
25+
].join '\n'
26+
expect(Diamond.rows 'C').toEqual expected
27+
28+
xit 'Smallest non-degenerate case with even diamond side length', ->
29+
expected = [
30+
' A '
31+
' B B '
32+
' C C '
33+
'D D'
34+
' C C '
35+
' B B '
36+
' A '
37+
].join '\n'
38+
expect(Diamond.rows 'D').toEqual expected
39+
40+
xit 'Largest possible diamond', ->
41+
expected = [
42+
' A '
43+
' B B '
44+
' C C '
45+
' D D '
46+
' E E '
47+
' F F '
48+
' G G '
49+
' H H '
50+
' I I '
51+
' J J '
52+
' K K '
53+
' L L '
54+
' M M '
55+
' N N '
56+
' O O '
57+
' P P '
58+
' Q Q '
59+
' R R '
60+
' S S '
61+
' T T '
62+
' U U '
63+
' V V '
64+
' W W '
65+
' X X '
66+
' Y Y '
67+
'Z Z'
68+
' Y Y '
69+
' X X '
70+
' W W '
71+
' V V '
72+
' U U '
73+
' T T '
74+
' S S '
75+
' R R '
76+
' Q Q '
77+
' P P '
78+
' O O '
79+
' N N '
80+
' M M '
81+
' L L '
82+
' K K '
83+
' J J '
84+
' I I '
85+
' H H '
86+
' G G '
87+
' F F '
88+
' E E '
89+
' D D '
90+
' C C '
91+
' B B '
92+
' A '
93+
].join '\n'
94+
expect(Diamond.rows 'Z').toEqual expected

0 commit comments

Comments
 (0)