Skip to content
This repository was archived by the owner on May 30, 2023. It is now read-only.

Commit 9380793

Browse files
Add diamond exercise (#121)
1 parent c564fe2 commit 9380793

File tree

8 files changed

+207
-0
lines changed

8 files changed

+207
-0
lines changed

config.json

+10
Original file line numberDiff line numberDiff line change
@@ -585,6 +585,16 @@
585585
],
586586
"difficulty": 2
587587
},
588+
{
589+
"slug": "diamond",
590+
"name": "Diamond",
591+
"uuid": "1c0d90ee-6b9e-425e-bd26-7f28026e601e",
592+
"practices": [],
593+
"prerequisites": [
594+
"strings"
595+
],
596+
"difficulty": 5
597+
},
588598
{
589599
"slug": "bank-account",
590600
"name": "Bank Account",
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+
"ErikSchierboom"
4+
],
5+
"files": {
6+
"solution": [
7+
"src/diamond.cljs"
8+
],
9+
"test": [
10+
"test/diamond_test.cljs"
11+
],
12+
"example": [
13+
".meta/src/example.cljs"
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,14 @@
1+
(ns diamond)
2+
3+
(defn- row [letter-count [row letter]]
4+
(let [outer-spaces (apply str (repeat (- letter-count row 1) " "))
5+
inner-spaces (apply str (repeat (if (zero? row) row (dec (* row 2))) " "))]
6+
(if (= letter \A)
7+
(str outer-spaces letter outer-spaces)
8+
(str outer-spaces letter inner-spaces letter outer-spaces))))
9+
10+
(defn diamond [letter]
11+
(let [letters (take-while #(>= 0 (compare % letter)) "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
12+
indexed-letters (map-indexed vector letters)
13+
row-letters (concat indexed-letters (rest (reverse indexed-letters)))]
14+
(map #(row (count letters) %) row-letters)))
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# This is an auto-generated file. Regular comments will be removed when this
2+
# file is regenerated. Regenerating will not touch any manually added keys,
3+
# so comments can be added in a "comment" key.
4+
5+
[202fb4cc-6a38-4883-9193-a29d5cb92076]
6+
description = "Degenerate case with a single 'A' row"
7+
8+
[bd6a6d78-9302-42e9-8f60-ac1461e9abae]
9+
description = "Degenerate case with no row containing 3 distinct groups of spaces"
10+
11+
[af8efb49-14ed-447f-8944-4cc59ce3fd76]
12+
description = "Smallest non-degenerate case with odd diamond side length"
13+
14+
[e0c19a95-9888-4d05-86a0-fa81b9e70d1d]
15+
description = "Smallest non-degenerate case with even diamond side length"
16+
17+
[82ea9aa9-4c0e-442a-b07e-40204e925944]
18+
description = "Largest possible diamond"

exercises/practice/diamond/deps.edn

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{:deps
2+
{org.clojure/clojure {:mvn/version "1.10.1"}
3+
org.clojure/clojurescript {:mvn/version "1.10.773"}}
4+
5+
:aliases
6+
{:test
7+
{:extra-paths ["test"]
8+
:extra-deps
9+
{olical/cljs-test-runner {:mvn/version "3.8.0"}}
10+
:main-opts ["-m" "cljs-test-runner.main"]}}}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
(ns diamond)
2+
3+
(defn diamond [] ;; <- arglist goes here
4+
;; your code goes here
5+
)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
(ns diamond-test
2+
(:require [clojure.test :refer [deftest is are]]
3+
[diamond :refer [diamond]]))
4+
5+
(deftest single-a-row
6+
(is (= (diamond \A) ["A"])))
7+
8+
(deftest b-diamond
9+
(is (= (diamond \B) [" A "
10+
"B B"
11+
" A "])))
12+
(deftest c-diamond
13+
(is (= (diamond \C) [" A "
14+
" B B "
15+
"C C"
16+
" B B "
17+
" A "])))
18+
19+
(deftest d-diamond
20+
(is (= (diamond \D) [" A "
21+
" B B "
22+
" C C "
23+
"D D"
24+
" C C "
25+
" B B "
26+
" A "])))
27+
28+
(deftest full-z-diamond
29+
(is (= (diamond \Z) [" A "
30+
" B B "
31+
" C C "
32+
" D D "
33+
" E E "
34+
" F F "
35+
" G G "
36+
" H H "
37+
" I I "
38+
" J J "
39+
" K K "
40+
" L L "
41+
" M M "
42+
" N N "
43+
" O O "
44+
" P P "
45+
" Q Q "
46+
" R R "
47+
" S S "
48+
" T T "
49+
" U U "
50+
" V V "
51+
" W W "
52+
" X X "
53+
" Y Y "
54+
"Z Z"
55+
" Y Y "
56+
" X X "
57+
" W W "
58+
" V V "
59+
" U U "
60+
" T T "
61+
" S S "
62+
" R R "
63+
" Q Q "
64+
" P P "
65+
" O O "
66+
" N N "
67+
" M M "
68+
" L L "
69+
" K K "
70+
" J J "
71+
" I I "
72+
" H H "
73+
" G G "
74+
" F F "
75+
" E E "
76+
" D D "
77+
" C C "
78+
" B B "
79+
" A "])))

0 commit comments

Comments
 (0)