Skip to content

Commit

Permalink
adding diamond
Browse files Browse the repository at this point in the history
  • Loading branch information
kapitaali committed May 13, 2024
1 parent c5ce548 commit a027603
Show file tree
Hide file tree
Showing 11 changed files with 585 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,14 @@
"prerequisites": [],
"difficulty": 3
},
{
"slug": "diamond",
"name": "Diamond",
"uuid": "b8e1e5e1-6d46-4601-af80-c60c54401bf8",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "secret-handshake",
"name": "Secret Handshake",
Expand Down
53 changes: 53 additions & 0 deletions exercises/practice/diamond/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# 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····
```
26 changes: 26 additions & 0 deletions exercises/practice/diamond/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"authors": [
"kapitaali"
],
"files": {
"solution": [
"src/diamond.cob"
],
"test": [
"tst/diamond/diamond.cut"
],
"example": [
".meta/proof.ci.cob"
],
"invalidator": [
"test.ps1",
"test.sh",
"bin/fetch-cobolcheck",
"bin/fetch-cobolcheck.ps1",
"config.properties"
]
},
"blurb": "Given a letter, print a diamond starting with 'A' with the supplied letter at the widest point.",
"source": "Seb Rose",
"source_url": "http://claysnow.co.uk/recycling-tests-in-tdd/"
}
77 changes: 77 additions & 0 deletions exercises/practice/diamond/.meta/proof.ci.cob
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. DIAMOND.
AUTHOR. kapitaali.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUTVARS.
05 WS-LETTER PIC X.
05 WS-ROWS PIC 99.
01 WS-OUTPUTTABLE.
05 WS-TABLEROW OCCURS 1 TO 60 DEPENDING ON WS-ROWS.
10 WS-LINE PIC X(60).

01 MY-WORKVARS.
05 CHARS PIC X(26) VALUE 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'.
05 ALINE PIC X(60).
05 MIDDLEPOINT PIC 99.
05 A PIC 99.
05 B PIC 99.
05 C PIC 99.
05 D PIC 99.
05 COUNTER PIC 99.
05 ADD-THIS PIC S99.


PROCEDURE DIVISION.

GET-TABLE-SIZE.
PERFORM VARYING A FROM 1 BY 1 UNTIL A = 27
IF CHARS(A:1) = WS-LETTER
EXIT PARAGRAPH
END-IF
END-PERFORM.


DIAMOND.
EVALUATE WS-LETTER
WHEN 'A'
MOVE 1 TO WS-ROWS
INITIALIZE WS-OUTPUTTABLE
MOVE 'A' TO WS-TABLEROW(1)
WHEN 'B'
MOVE 3 TO WS-ROWS
INITIALIZE WS-OUTPUTTABLE
MOVE " A " TO WS-TABLEROW(1)
MOVE "B B" TO WS-TABLEROW(2)
MOVE " A " TO WS-TABLEROW(3)
WHEN OTHER
PERFORM GET-TABLE-SIZE
COMPUTE WS-ROWS = 2 + 2*(A - 2) + 1
COMPUTE MIDDLEPOINT = (WS-ROWS - 1) / 2
INITIALIZE WS-OUTPUTTABLE
MOVE SPACES TO ALINE
COMPUTE D = MIDDLEPOINT + 1
MOVE CHARS(1:1) TO ALINE(D:1)
MOVE ALINE TO WS-TABLEROW(1)
MOVE ALINE TO WS-TABLEROW(WS-ROWS)
MOVE 0 TO COUNTER
MOVE 1 TO ADD-THIS
PERFORM VARYING A FROM 2 BY 1 UNTIL A = WS-ROWS
MOVE SPACES TO ALINE
IF COUNTER = MIDDLEPOINT
MOVE -1 TO ADD-THIS
END-IF
COMPUTE COUNTER = COUNTER + ADD-THIS
COMPUTE B = MIDDLEPOINT - COUNTER
COMPUTE C = COUNTER + 1
COMPUTE D = B + 1
MOVE CHARS(C:1) TO ALINE(D:1)
COMPUTE B = MIDDLEPOINT + COUNTER
COMPUTE C = COUNTER + 1
COMPUTE D = B + 1
MOVE CHARS(C:1) TO ALINE(D:1)
MOVE ALINE TO WS-TABLEROW(A)
END-PERFORM
END-EVALUATE.

63 changes: 63 additions & 0 deletions exercises/practice/diamond/bin/fetch-cobolcheck
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env bash

# This file is a copy of the
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet file.
# Please submit bugfixes/improvements to the above file to ensure that all tracks benefit from the changes.

# set -eo pipefail

readonly LATEST='https://api.github.com/repos/0xE282B0/cobol-check/releases/latest'

case "$(uname)" in
Darwin*) os='mac' ;;
Linux*) os='linux' ;;
Windows*) os='windows' ;;
MINGW*) os='windows' ;;
MSYS_NT-*) os='windows' ;;
*) os='linux' ;;
esac

case "${os}" in
windows*) ext='.exe' ;;
*) ext='' ;;
esac

arch="$(uname -m)"

curlopts=(
--silent
--show-error
--fail
--location
--retry 3
)

if [[ -n "${GITHUB_TOKEN}" ]]; then
curlopts+=(--header "authorization: Bearer ${GITHUB_TOKEN}")
fi

suffix="${os}-${arch}${ext}"

get_download_url() {
curl "${curlopts[@]}" --header 'Accept: application/vnd.github.v3+json' "${LATEST}" |
grep "\"browser_download_url\": \".*/download/.*/cobol-check.*${suffix}\"$" |
cut -d'"' -f4
}

main() {
if [[ -d ./bin ]]; then
output_dir="./bin"
elif [[ $PWD == */bin ]]; then
output_dir="$PWD"
else
echo "Error: no ./bin directory found. This script should be ran from a repo root." >&2
return 1
fi

output_path="${output_dir}/cobolcheck${ext}"
download_url="$(get_download_url)"
curl "${curlopts[@]}" --output "${output_path}" "${download_url}"
chmod +x "${output_path}"
}

main
28 changes: 28 additions & 0 deletions exercises/practice/diamond/bin/fetch-cobolcheck.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# This file is a copy of the
# https://github.com/exercism/configlet/blob/main/scripts/fetch-configlet.ps1 file.
# Please submit bugfixes/improvements to the above file to ensure that all tracks
# benefit from the changes.

$ErrorActionPreference = "Stop"
$ProgressPreference = "SilentlyContinue"

$requestOpts = @{
Headers = If ($env:GITHUB_TOKEN) { @{ Authorization = "Bearer ${env:GITHUB_TOKEN}" } } Else { @{ } }
MaximumRetryCount = 3
RetryIntervalSec = 1
}

$arch = If ([Environment]::Is64BitOperatingSystem) { "amd64" } Else { "x86" }
$fileName = "cobol-check-windows-$arch.exe"

Function Get-DownloadUrl {
$latestUrl = "https://api.github.com/repos/0xE282B0/cobol-check/releases/latest"
Invoke-RestMethod -Uri $latestUrl -PreserveAuthorizationOnRedirect @requestOpts
| Select-Object -ExpandProperty assets
| Where-Object { $_.browser_download_url -match $FileName }
| Select-Object -ExpandProperty browser_download_url
}

$downloadUrl = Get-DownloadUrl
$outputFile = Join-Path -Path $PSScriptRoot -ChildPath "cobolcheck.exe"
Invoke-WebRequest -Uri $downloadUrl -OutFile $outputFile @requestOpts
Loading

0 comments on commit a027603

Please sign in to comment.