Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement exercise 'diamond' #24

Merged
merged 1 commit into from
Sep 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,14 @@
"prerequisites": [],
"difficulty": 5
},
{
"slug": "diamond",
"name": "Diamond",
"uuid": "b907e51b-3272-4fb7-b43c-09bcf2005fbd",
"practices": [],
"prerequisites": [],
"difficulty": 5
},
{
"slug": "matching-brackets",
"name": "Matching Brackets",
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -310,6 +310,13 @@ exercises:
prerequisites: []
difficulty: 5

- slug: diamond
name: Diamond
uuid: b907e51b-3272-4fb7-b43c-09bcf2005fbd
practices: []
prerequisites: []
difficulty: 5

- slug: matching-brackets
name: Matching Brackets
uuid: a6c79db6-925c-45c3-8a59-aa9d96a4740b
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····
```
1 change: 1 addition & 0 deletions exercises/practice/diamond/.meta/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.76
28 changes: 28 additions & 0 deletions exercises/practice/diamond/.meta/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
SHELL := bash

BASE := $(shell pwd)

export YS_VERSION := 0.1.76

YS_LOCAL_PREFIX := ../../../../.local/v$(YS_VERSION)

YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin

YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION)

TEST_FILE ?= $(wildcard *-test.ys)


export PATH := $(YS_LOCAL_BIN):$(PATH)

export YSPATH := $(BASE)


default:

test: $(YS_BIN)
prove -v $(TEST_FILE)

$(YS_BIN):
curl -s https://yamlscript.org/install | \
BIN=1 VERSION=$(YS_VERSION) PREFIX=$(YS_LOCAL_PREFIX) bash >/dev/null
23 changes: 23 additions & 0 deletions exercises/practice/diamond/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"ingydotnet"
],
"files": {
"solution": [
"diamond.ys"
],
"test": [
"diamond-test.ys",
"GNUmakefile",
"Makefile",
".yamlscript/exercise.mk",
".yamlscript/exercism-ys-installer"
],
"example": [
".meta/diamond.ys"
]
},
"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/"
}
100 changes: 100 additions & 0 deletions exercises/practice/diamond/.meta/diamond-test.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
#!/usr/bin/env ys-0

require ys::taptest: :all

use: diamond

test::
- name: Degenerate case with a single 'a' row
code: rows("A")
want: |2
A
uuid: 202fb4cc-6a38-4883-9193-a29d5cb92076

- name: Degenerate case with no row containing 3 distinct groups of spaces
code: rows("B")
want: |2
A
B B
A
uuid: bd6a6d78-9302-42e9-8f60-ac1461e9abae

- name: Smallest non-degenerate case with odd diamond side length
code: rows("C")
want: |2
A
B B
C C
B B
A
uuid: af8efb49 14ed-447f-8944-4cc59ce3fd76

- name: Smallest non-degenerate case with even diamond side length
code: rows("D")
want: |2
A
B B
C C
D D
C C
B B
A
uuid: e0c19a95 9888-4d05-86a0-fa81b9e70d1d

- name: Largest possible diamond
code: rows("Z")
want: |2
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
uuid: 82ea9aa9 4c0e-442a-b07e-40204e925944

done: 5
7 changes: 7 additions & 0 deletions exercises/practice/diamond/.meta/diamond.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
!yamlscript/v0

defn rows(letter):
loop C letter.0, I (' ' * (((-64 + C) * 2) - 3)), O '', dmnd '':
line =: if(I.? "$O$C$I$C$O\n" "$O$C$O\n")
dmnd =: if(dmnd.? "$line$dmnd$line" line)
if I.?: recur(C.--, I.drop(2).join(), "$O ", dmnd), dmnd
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"
1 change: 1 addition & 0 deletions exercises/practice/diamond/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
Loading