Skip to content

Commit

Permalink
Add diamond exercise (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
keiravillekode authored Jun 2, 2024
1 parent 7c61f76 commit 3ba948b
Show file tree
Hide file tree
Showing 7 changed files with 240 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -535,6 +535,14 @@
"math"
]
},
{
"slug": "diamond",
"name": "Diamond",
"uuid": "312bff7d-34f4-4ade-bbd3-479bce9bff58",
"practices": [],
"prerequisites": [],
"difficulty": 4
},
{
"slug": "largest-series-product",
"name": "Largest Series Product",
Expand Down
52 changes: 52 additions & 0 deletions exercises/practice/diamond/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# Instructions

The diamond kata takes as its input a letter, and outputs it in a diamond shape.
Given a letter, it prints a diamond starting with 'A', with the supplied letter at the widest point.

## Requirements

- The first row contains one 'A'.
- The last row contains one 'A'.
- All rows, except the first and last, have exactly two identical letters.
- All rows have as many trailing spaces as leading spaces. (This might be 0).
- The diamond is horizontally symmetric.
- The diamond is vertically symmetric.
- The diamond has a square shape (width equals height).
- The letters form a diamond shape.
- The top half has the letters in ascending order.
- The bottom half has the letters in descending order.
- The four corners (containing the spaces) are triangles.

## Examples

In the following examples, spaces are indicated by `·` characters.

Diamond for letter 'A':

```text
A
```

Diamond for letter 'C':

```text
··A··
·B·B·
C···C
·B·B·
··A··
```

Diamond for letter 'E':

```text
····A····
···B·B···
··C···C··
·D·····D·
E·······E
·D·····D·
··C···C··
···B·B···
····A····
```
19 changes: 19 additions & 0 deletions exercises/practice/diamond/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"keiravillekode"
],
"files": {
"solution": [
"diamond.el"
],
"test": [
"diamond-test.el"
],
"example": [
".meta/example.el"
]
},
"blurb": "Given a letter, print a diamond starting with 'A' with the supplied letter at the widest point.",
"source": "Seb Rose",
"source_url": "https://web.archive.org/web/20220807163751/http://claysnow.co.uk/recycling-tests-in-tdd/"
}
27 changes: 27 additions & 0 deletions exercises/practice/diamond/.meta/example.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
;;; diamond.el --- Diamond (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:

(require 'cl-lib)

(defun rows (letter)
(let* ((n (- letter ?A))
(length (1+ (* 2 n)))
(result (make-vector length nil)))
(cl-labels
((row (index)
(let ((str (make-string length 32)))
(aset str (- n index) (+ ?A index))
(aset str (+ n index) (+ ?A index))
str)))
(cl-loop for index to n
do (aset result index (funcall #'row index)))
(cl-loop for index below n
do (aset result (- length 1 index) (funcall #'row index)))
result)))

(provide 'diamond)
;;; diamond.el ends here

25 changes: 25 additions & 0 deletions exercises/practice/diamond/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# This is an auto-generated file.
#
# Regenerating this file via `configlet sync` will:
# - Recreate every `description` key/value pair
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
# - Preserve any other key/value pair
#
# As user-added comments (using the # character) will be removed when this file
# is regenerated, comments can be added via a `comment` key.

[202fb4cc-6a38-4883-9193-a29d5cb92076]
description = "Degenerate case with a single 'A' row"

[bd6a6d78-9302-42e9-8f60-ac1461e9abae]
description = "Degenerate case with no row containing 3 distinct groups of spaces"

[af8efb49-14ed-447f-8944-4cc59ce3fd76]
description = "Smallest non-degenerate case with odd diamond side length"

[e0c19a95-9888-4d05-86a0-fa81b9e70d1d]
description = "Smallest non-degenerate case with even diamond side length"

[82ea9aa9-4c0e-442a-b07e-40204e925944]
description = "Largest possible diamond"
95 changes: 95 additions & 0 deletions exercises/practice/diamond/diamond-test.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
;;; diamond-test.el --- Tests for Diamond (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(load-file "diamond.el")
(declare-function rows "diamond.el" (letter))


(ert-deftest degenerate-case-with-a-single-a-row ()
(should (equal ["A"] (rows ?A))))


(ert-deftest degenerate-case-with-no-row-containing-3-distinct-groups-of-spaces ()
(should (equal [" A "
"B B"
" A "] (rows ?B))))


(ert-deftest smallest-non-degenerate-case-with-odd-diamond-side-length ()
(should (equal [" A "
" B B "
"C C"
" B B "
" A "] (rows ?C))))


(ert-deftest smallest-non-degenerate-case-with-even-diamond-side-length ()
(should (equal [" A "
" B B "
" C C "
"D D"
" C C "
" B B "
" A "] (rows ?D))))


(ert-deftest largest-possible-diamond ()
(should (equal [" A "
" B B "
" C C "
" D D "
" E E "
" F F "
" G G "
" H H "
" I I "
" J J "
" K K "
" L L "
" M M "
" N N "
" O O "
" P P "
" Q Q "
" R R "
" S S "
" T T "
" U U "
" V V "
" W W "
" X X "
" Y Y "
"Z Z"
" Y Y "
" X X "
" W W "
" V V "
" U U "
" T T "
" S S "
" R R "
" Q Q "
" P P "
" O O "
" N N "
" M M "
" L L "
" K K "
" J J "
" I I "
" H H "
" G G "
" F F "
" E E "
" D D "
" C C "
" B B "
" A "] (rows ?Z))))


(provide 'diamond-test)
;;; diamond-test.el ends here
14 changes: 14 additions & 0 deletions exercises/practice/diamond/diamond.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
;;; diamond.el --- Diamond (exercism) -*- lexical-binding: t; -*-

;;; Commentary:

;;; Code:


(defun rows (letter)
(error "Delete this S-Expression and write your own implementation"))


(provide 'diamond)
;;; diamond.el ends here

0 comments on commit 3ba948b

Please sign in to comment.