Skip to content

Commit

Permalink
Add new practice exercise diamond (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
jiegillet authored Aug 30, 2024
1 parent 4511171 commit 530027b
Show file tree
Hide file tree
Showing 8 changed files with 270 additions and 0 deletions.
14 changes: 14 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
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": [
"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/"
}
27 changes: 27 additions & 0 deletions exercises/practice/diamond/.meta/src/Diamond.example.elm
Original file line number Diff line number Diff line change
@@ -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'
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"
29 changes: 29 additions & 0 deletions exercises/practice/diamond/elm.json
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
6 changes: 6 additions & 0 deletions exercises/practice/diamond/src/Diamond.elm
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module Diamond exposing (rows)


rows : Char -> String
rows letter =
Debug.todo "Please implement rows"
98 changes: 98 additions & 0 deletions exercises/practice/diamond/tests/Tests.elm
Original file line number Diff line number Diff line change
@@ -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_________________________"""
]

0 comments on commit 530027b

Please sign in to comment.