From 48fc40a7726595489b79a7cdfa7604dec03be9e4 Mon Sep 17 00:00:00 2001 From: BNAndras <20251272+BNAndras@users.noreply.github.com> Date: Sun, 8 Mar 2026 23:11:06 -0700 Subject: [PATCH 1/2] Initial files for `diamond` --- config.json | 8 ++ exercises/practice/diamond/.busted | 5 + .../practice/diamond/.docs/instructions.md | 52 ++++++++++ exercises/practice/diamond/.meta/config.json | 19 ++++ exercises/practice/diamond/.meta/example.moon | 4 + .../diamond/.meta/spec_generator.moon | 16 +++ exercises/practice/diamond/.meta/tests.toml | 25 +++++ exercises/practice/diamond/diamond.moon | 4 + exercises/practice/diamond/diamond_spec.moon | 99 +++++++++++++++++++ 9 files changed, 232 insertions(+) create mode 100644 exercises/practice/diamond/.busted create mode 100644 exercises/practice/diamond/.docs/instructions.md create mode 100644 exercises/practice/diamond/.meta/config.json create mode 100644 exercises/practice/diamond/.meta/example.moon create mode 100644 exercises/practice/diamond/.meta/spec_generator.moon create mode 100644 exercises/practice/diamond/.meta/tests.toml create mode 100644 exercises/practice/diamond/diamond.moon create mode 100644 exercises/practice/diamond/diamond_spec.moon diff --git a/config.json b/config.json index 6050599..8ae3af5 100644 --- a/config.json +++ b/config.json @@ -354,6 +354,14 @@ "prerequisites": [], "difficulty": 4 }, + { + "slug": "diamond", + "name": "Diamond", + "uuid": "a3ea5637-c295-40ee-9ebf-a64268c60c77", + "practices": [], + "prerequisites": [], + "difficulty": 4 + }, { "slug": "isbn-verifier", "name": "ISBN Verifier", diff --git a/exercises/practice/diamond/.busted b/exercises/practice/diamond/.busted new file mode 100644 index 0000000..86b84e7 --- /dev/null +++ b/exercises/practice/diamond/.busted @@ -0,0 +1,5 @@ +return { + default = { + ROOT = { '.' } + } +} diff --git a/exercises/practice/diamond/.docs/instructions.md b/exercises/practice/diamond/.docs/instructions.md new file mode 100644 index 0000000..3034802 --- /dev/null +++ b/exercises/practice/diamond/.docs/instructions.md @@ -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···· +``` diff --git a/exercises/practice/diamond/.meta/config.json b/exercises/practice/diamond/.meta/config.json new file mode 100644 index 0000000..9c47219 --- /dev/null +++ b/exercises/practice/diamond/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "diamond.moon" + ], + "test": [ + "diamond_spec.moon" + ], + "example": [ + ".meta/example.moon" + ] + }, + "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/" +} diff --git a/exercises/practice/diamond/.meta/example.moon b/exercises/practice/diamond/.meta/example.moon new file mode 100644 index 0000000..3a9ee4d --- /dev/null +++ b/exercises/practice/diamond/.meta/example.moon @@ -0,0 +1,4 @@ +{ + rows: (letter) -> + error 'Implement me!' +} diff --git a/exercises/practice/diamond/.meta/spec_generator.moon b/exercises/practice/diamond/.meta/spec_generator.moon new file mode 100644 index 0000000..837b715 --- /dev/null +++ b/exercises/practice/diamond/.meta/spec_generator.moon @@ -0,0 +1,16 @@ +{ + module_name: 'Diamond' + + generate_test: (case, level) -> + lines = {} + table.insert lines, "result = Diamond.#{case.property} #{quote case.input.letter}" + + rows = [indent quote(row), level + 1 for row in *case.expected] + table.insert rows, 1, '{' + table.insert rows, indent('}', level) + + table.insert lines, "expected = #{table.concat rows, '\n'}" + table.insert lines, "assert.are.same expected, result" + + table.concat [indent line, level for line in *lines], '\n' +} diff --git a/exercises/practice/diamond/.meta/tests.toml b/exercises/practice/diamond/.meta/tests.toml new file mode 100644 index 0000000..4e7802e --- /dev/null +++ b/exercises/practice/diamond/.meta/tests.toml @@ -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" diff --git a/exercises/practice/diamond/diamond.moon b/exercises/practice/diamond/diamond.moon new file mode 100644 index 0000000..3a9ee4d --- /dev/null +++ b/exercises/practice/diamond/diamond.moon @@ -0,0 +1,4 @@ +{ + rows: (letter) -> + error 'Implement me!' +} diff --git a/exercises/practice/diamond/diamond_spec.moon b/exercises/practice/diamond/diamond_spec.moon new file mode 100644 index 0000000..8c9f0be --- /dev/null +++ b/exercises/practice/diamond/diamond_spec.moon @@ -0,0 +1,99 @@ +Diamond = require 'diamond' + +describe 'diamond', -> + it "Degenerate case with a single 'A' row", -> + result = Diamond.rows 'A' + expected = { + 'A' + } + assert.are.same expected, result + + pending 'Degenerate case with no row containing 3 distinct groups of spaces', -> + result = Diamond.rows 'B' + expected = { + ' A ' + 'B B' + ' A ' + } + assert.are.same expected, result + + pending 'Smallest non-degenerate case with odd diamond side length', -> + result = Diamond.rows 'C' + expected = { + ' A ' + ' B B ' + 'C C' + ' B B ' + ' A ' + } + assert.are.same expected, result + + pending 'Smallest non-degenerate case with even diamond side length', -> + result = Diamond.rows 'D' + expected = { + ' A ' + ' B B ' + ' C C ' + 'D D' + ' C C ' + ' B B ' + ' A ' + } + assert.are.same expected, result + + pending 'Largest possible diamond', -> + result = Diamond.rows 'Z' + expected = { + ' 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 ' + } + assert.are.same expected, result From 0d0dab3b206a4df7e68beb756415b9bc43756935 Mon Sep 17 00:00:00 2001 From: BNAndras <20251272+BNAndras@users.noreply.github.com> Date: Mon, 9 Mar 2026 08:22:27 -0700 Subject: [PATCH 2/2] Add example solution --- exercises/practice/diamond/.meta/example.moon | 23 ++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/exercises/practice/diamond/.meta/example.moon b/exercises/practice/diamond/.meta/example.moon index 3a9ee4d..e67c7aa 100644 --- a/exercises/practice/diamond/.meta/example.moon +++ b/exercises/practice/diamond/.meta/example.moon @@ -1,4 +1,25 @@ { rows: (letter) -> - error 'Implement me!' + return {'A'} if letter == 'A' + + first = string.byte 'A' + last = string.byte letter + lines = last - first + 1 + diamond = {} + + for i = 0, lines - 1 + symbol = string.char first + i + outerPadding = string.rep ' ', lines - 1 - i + line = if i == 0 + outerPadding .. symbol .. outerPadding + else + innerPadding = string.rep ' ', 2 * i - 1 + outerPadding .. symbol .. innerPadding .. symbol .. outerPadding + table.insert diamond, line + + -- flip the diamond + for i = lines - 1, 1, -1 + table.insert diamond, diamond[i] + + diamond }