From 530027bda024a8c58d6c538025ed7bfc991ef1a5 Mon Sep 17 00:00:00 2001 From: Jie Date: Fri, 30 Aug 2024 17:21:03 +0200 Subject: [PATCH] Add new practice exercise `diamond` (#710) --- config.json | 14 +++ .../practice/diamond/.docs/instructions.md | 52 ++++++++++ exercises/practice/diamond/.meta/config.json | 19 ++++ .../diamond/.meta/src/Diamond.example.elm | 27 +++++ exercises/practice/diamond/.meta/tests.toml | 25 +++++ exercises/practice/diamond/elm.json | 29 ++++++ exercises/practice/diamond/src/Diamond.elm | 6 ++ exercises/practice/diamond/tests/Tests.elm | 98 +++++++++++++++++++ 8 files changed, 270 insertions(+) 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/src/Diamond.example.elm create mode 100644 exercises/practice/diamond/.meta/tests.toml create mode 100644 exercises/practice/diamond/elm.json create mode 100644 exercises/practice/diamond/src/Diamond.elm create mode 100644 exercises/practice/diamond/tests/Tests.elm diff --git a/config.json b/config.json index 94eab478..7547b1b0 100644 --- a/config.json +++ b/config.json @@ -1310,6 +1310,20 @@ ], "difficulty": 7 }, + { + "slug": "diamond", + "name": "Diamond", + "uuid": "0c2a3fce-231a-43db-98df-84fcc74c3c97", + "practices": [ + "lists" + ], + "prerequisites": [ + "strings", + "lists", + "let" + ], + "difficulty": 4 + }, { "slug": "spiral-matrix", "name": "Spiral Matrix", diff --git a/exercises/practice/diamond/.docs/instructions.md b/exercises/practice/diamond/.docs/instructions.md new file mode 100644 index 00000000..3034802f --- /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 00000000..88f8f162 --- /dev/null +++ b/exercises/practice/diamond/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "jiegillet" + ], + "files": { + "solution": [ + "src/Diamond.elm" + ], + "test": [ + "tests/Tests.elm" + ], + "example": [ + ".meta/src/Diamond.example.elm" + ] + }, + "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/src/Diamond.example.elm b/exercises/practice/diamond/.meta/src/Diamond.example.elm new file mode 100644 index 00000000..a16f64e0 --- /dev/null +++ b/exercises/practice/diamond/.meta/src/Diamond.example.elm @@ -0,0 +1,27 @@ +module Diamond exposing (rows) + +import Char + + +rows : Char -> String +rows letter = + let + side = + Char.toCode letter - aCode + + lines = + List.range 0 side + |> List.map + (\i -> + String.repeat (2 * i - 1) "_" + |> String.pad (2 * i + 1) (Char.fromCode (aCode + i)) + |> String.pad (2 * side + 1) '_' + ) + in + (lines ++ (lines |> List.reverse |> List.drop 1)) + |> String.join "\n" + + +aCode : Int +aCode = + Char.toCode 'A' diff --git a/exercises/practice/diamond/.meta/tests.toml b/exercises/practice/diamond/.meta/tests.toml new file mode 100644 index 00000000..4e7802ec --- /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/elm.json b/exercises/practice/diamond/elm.json new file mode 100644 index 00000000..22c9e137 --- /dev/null +++ b/exercises/practice/diamond/elm.json @@ -0,0 +1,29 @@ +{ + "type": "application", + "source-directories": [ + "src" + ], + "elm-version": "0.19.1", + "dependencies": { + "direct": { + "elm/core": "1.0.5", + "elm/json": "1.1.3", + "elm/parser": "1.1.0", + "elm/random": "1.0.0", + "elm/regex": "1.0.0", + "elm/time": "1.0.0" + }, + "indirect": {} + }, + "test-dependencies": { + "direct": { + "elm-explorations/test": "2.1.0", + "rtfeldman/elm-iso8601-date-strings": "1.1.4" + }, + "indirect": { + "elm/bytes": "1.0.8", + "elm/html": "1.0.0", + "elm/virtual-dom": "1.0.3" + } + } +} diff --git a/exercises/practice/diamond/src/Diamond.elm b/exercises/practice/diamond/src/Diamond.elm new file mode 100644 index 00000000..63bfcbcc --- /dev/null +++ b/exercises/practice/diamond/src/Diamond.elm @@ -0,0 +1,6 @@ +module Diamond exposing (rows) + + +rows : Char -> String +rows letter = + Debug.todo "Please implement rows" diff --git a/exercises/practice/diamond/tests/Tests.elm b/exercises/practice/diamond/tests/Tests.elm new file mode 100644 index 00000000..cbc4f7e6 --- /dev/null +++ b/exercises/practice/diamond/tests/Tests.elm @@ -0,0 +1,98 @@ +module Tests exposing (tests) + +import Diamond +import Expect +import Test exposing (Test, describe, skip, test) + + +tests : Test +tests = + describe "Diamond" + [ -- skip <| + test "Degenerate case with a single 'A' row" <| + \() -> + Diamond.rows 'A' + |> Expect.equal "A" + , skip <| + test "Degenerate case with no row containing 3 distinct groups of spaces" <| + \() -> + Diamond.rows 'B' + |> Expect.equal """_A_ +B_B +_A_""" + , skip <| + test "Smallest non-degenerate case with odd diamond side length" <| + \() -> + Diamond.rows 'C' + |> Expect.equal """__A__ +_B_B_ +C___C +_B_B_ +__A__""" + , skip <| + test "Smallest non-degenerate case with even diamond side length" <| + \() -> + Diamond.rows 'D' + |> Expect.equal """___A___ +__B_B__ +_C___C_ +D_____D +_C___C_ +__B_B__ +___A___""" + , skip <| + test "Largest possible diamond" <| + \() -> + Diamond.rows 'Z' + |> Expect.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_________________________""" + ]