Diamond Kata
Level: Intermediate 30–60 minConcepts: AlgorithmsStringsIncremental Design
Solutions: C# | TypeScript | Python
Given a letter, generate a diamond shape that starts with A, widens to the given letter, then narrows back to A.
Requirements
Given A, return:
A
Given B, return:
A
B B
A
Given C, return:
A
B B
C C
B B
A
Given D, return:
A
B B
C C
D D
C C
B B
A
Rules
- The first and last rows contain a single
A - All rows except the first and last have exactly two identical letters
- The diamond is vertically and horizontally symmetric
- The width equals the height
- The top half is a mirror of the bottom half
- Leading spaces align the letters into a diamond shape
- Inner spaces between the two letters increase by 2 for each row from the top
Test Cases
| Input | Height | Width | First Row | Middle Row |
|---|---|---|---|---|
A | 1 | 1 | A | A |
B | 3 | 3 | ·A | B·B |
C | 5 | 5 | ··A | C···C |
E | 9 | 9 | ····A | E·······E |
(Dots represent spaces for clarity)
Bonus
- Accept lowercase input and normalize to uppercase
- Add a
hollowoption that only prints the border characters and fills the interior with spaces - Validate input is a single letter A-Z
Hint
Don’t try to build the whole diamond at once. Focus on generating a single row first — given a target letter and a current letter, produce the correct row with the right leading spaces and inner spaces. Once a single row works, the diamond is just a sequence of rows mirrored vertically.
Reference Walkthrough
Reference implementations in C#, TypeScript, and Python live at tddbuddy-reference-katas/diamond. This is an F1 kata — 10 shared scenarios covering the shape from A (a single letter) through Z (a full 51-row diamond), lowercase normalization, top/bottom-mirror and no-trailing-whitespace invariants, and rejection of non-A–Z input with the byte-identical message letter must be a single A-Z character.
- C# (.NET 8, xUnit, FluentAssertions 6.12.0) — walkthrough
- TypeScript (Node 20, Vitest 1.6, TS 5 strict) — walkthrough
- Python (3.11, pytest) — walkthrough
This kata ships in Agent Full-Bake mode at high gear: each row computes its offset from the diamond’s vertical midpoint (min(r, 2n - r)) and composes leading spaces + letter + 2 * offset - 1 inner spaces + letter; the top/bottom symmetry falls out of the offset formula rather than from explicit mirroring. One commit per language, with a brief walkthrough noting there are no builders because the algorithm’s inputs and outputs are the domain — no aggregates to construct, no value types to introduce, no collaborators to inject. Python names the function print_diamond to avoid shadowing the built-in print. See the repo’s Gears section for when high gear is the right call.