diff --git a/config.json b/config.json index cb10ed7..916b5c3 100644 --- a/config.json +++ b/config.json @@ -654,6 +654,14 @@ "practices": [], "prerequisites": [], "difficulty": 3 + }, + { + "slug": "diamond", + "name": "Diamond", + "uuid": "ec6e40c4-2aa6-4255-962d-fcbf129d7950", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, 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..ab59d78 --- /dev/null +++ b/exercises/practice/diamond/.meta/config.json @@ -0,0 +1,17 @@ +{ + "authors": ["blakelewis"], + "files": { + "solution": [ + "diamond.rkt" + ], + "test": [ + "diamond-test.rkt" + ], + "example": [ + ".meta/example.rkt" + ] + }, + "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.rkt b/exercises/practice/diamond/.meta/example.rkt new file mode 100644 index 0000000..80e2e35 --- /dev/null +++ b/exercises/practice/diamond/.meta/example.rkt @@ -0,0 +1,21 @@ +#lang racket + +(provide rows) + +(define (make-row n c) + (let* ([outside (make-string (- n c) #\space)] + [inside (make-string (max (sub1 (* 2 c)) 0) #\space)] + [mark (make-string 1 (integer->char (+ c (char->integer #\A))))] + [middle (if (zero? (string-length inside)) + mark + (string-append mark inside mark))]) + (string-append outside middle outside))) + + +(define (rows high-char) + (let* ([n (- (char->integer high-char) (char->integer #\A))] + [top-rows (for/list ([c (in-range n)]) + (make-row n c))] + [bottom-rows (reverse top-rows)] + [middle-row (make-row n n)]) + (append top-rows (list middle-row) bottom-rows))) 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-test.rkt b/exercises/practice/diamond/diamond-test.rkt new file mode 100644 index 0000000..e8a02dd --- /dev/null +++ b/exercises/practice/diamond/diamond-test.rkt @@ -0,0 +1,103 @@ +#lang racket/base + +(require "diamond.rkt") + +(module+ test + (require rackunit rackunit/text-ui)) + +(module+ test + (define suite + (test-suite "diamond tests" + + (test-equal? "Degenerate case with a single 'A' row" + (rows #\A) + '( + "A" + )) + + (test-equal? "Degenerate case with no row containing 3 distinct groups of spaces" + (rows #\B) + '( + " A " + "B B" + " A " + )) + + (test-equal? "Smallest non-degenerate case with odd diamond side length" + (rows #\C) + '( + " A " + " B B " + "C C" + " B B " + " A " + )) + + (test-equal? "Smallest non-degenerate case with even diamond side length" + (rows #\D) + '( + " A " + " B B " + " C C " + "D D" + " C C " + " B B " + " A " + )) + + (test-equal? "Largest possible diamond" + (rows #\Z) + '( + " 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 " + )))) + (run-tests suite)) diff --git a/exercises/practice/diamond/diamond.rkt b/exercises/practice/diamond/diamond.rkt new file mode 100644 index 0000000..2bc1784 --- /dev/null +++ b/exercises/practice/diamond/diamond.rkt @@ -0,0 +1,6 @@ +#lang racket + +(provide rows) + +(define (rows high-char) + (error "Not implemented yet"))