Skip to content

Commit

Permalink
adding secret-handshake
Browse files Browse the repository at this point in the history
  • Loading branch information
kapitaali committed Apr 22, 2024
1 parent d098126 commit 4b694b8
Show file tree
Hide file tree
Showing 12 changed files with 551 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": "secret-handshake",
"name": "Secret Handshake",
"uuid": "d643cdac-949f-430b-989e-bb0380627580",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "pascals-triangle",
"name": "Pascal's Triangle",
Expand Down
27 changes: 27 additions & 0 deletions exercises/practice/secret-handshake/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Instructions

> There are 10 types of people in the world: Those who understand
> binary, and those who don't.
You and your fellow cohort of those in the "know" when it comes to
binary decide to come up with a secret "handshake".

```text
00001 = wink
00010 = double blink
00100 = close your eyes
01000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the decimal input 3, the function would return the array
["wink", "double blink"] because the decimal number 3 is 2+1 in powers of two and thus `11` in binary.

Let's now examine the input 19 which is 16+2+1 in powers of two and thus `10011` in binary.
Recalling that the addition of 16 (`10000` in binary) reverses an array and that we already know what array is returned given input 3, the array returned for input 19 is ["double blink", "wink"].
27 changes: 27 additions & 0 deletions exercises/practice/secret-handshake/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Introduction

> There are 10 types of people in the world: Those who understand
> binary, and those who don't.
You and your fellow cohort of those in the "know" when it comes to
binary decide to come up with a secret "handshake".

```text
00001 = wink
00010 = double blink
00100 = close your eyes
01000 = jump
10000 = Reverse the order of the operations in the secret handshake.
```

Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.

Here's a couple of examples:

Given the decimal input 3, the function would return the array
["wink", "double blink"] because the decimal number 3 is 2+1 in powers of two and thus `11` in binary.

Let's now examine the input 19 which is 16+2+1 in powers of two and thus `10011` in binary.
Recalling that the addition of 16 (`10000` in binary) reverses an array and that we already know what array is returned given input 3, the array returned for input 19 is ["double blink", "wink"].
23 changes: 23 additions & 0 deletions exercises/practice/secret-handshake/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"authors": [
"kapitaali"
],
"files": {
"solution": [
"src/secret-handshake.cob"
],
"test": [
"tst/secret-handshake/secret-handshake.cut"
],
"example": [
".meta/proof.ci.cob"
],
"invalidator": [
"test.sh",
"test.ps1"
]
},
"blurb": "Given a decimal number, convert it to the appropriate sequence of events for a secret handshake.",
"source": "Bert, in Mary Poppins",
"source_url": "http://www.imdb.com/title/tt0058331/quotes/qt0437047"
}
90 changes: 90 additions & 0 deletions exercises/practice/secret-handshake/.meta/proof.ci.cob
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
IDENTIFICATION DIVISION.
PROGRAM-ID. SECRET-HANDSHAKE.
AUTHOR. kapitaali.
ENVIRONMENT DIVISION.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 WS-INPUT PIC 999.
01 WS-RESULT PIC X(60).
01 STR PIC X(5) VALUE '00000'.
01 CC PIC 99.

PROCEDURE DIVISION.

COMMANDS.
MOVE '00000' TO STR.
MOVE SPACES TO WS-RESULT.
IF WS-INPUT = 0
MOVE SPACES TO WS-RESULT
EXIT PARAGRAPH
END-IF.
IF WS-INPUT >= 16
SUBTRACT 16 FROM WS-INPUT
MOVE '1' TO STR(1:1)
END-IF.
IF WS-INPUT >= 8
SUBTRACT 8 FROM WS-INPUT
MOVE '1' TO STR(2:1)
END-IF.
IF WS-INPUT >= 4
SUBTRACT 4 FROM WS-INPUT
MOVE '1' TO STR(3:1)
END-IF.
IF WS-INPUT >= 2
SUBTRACT 2 FROM WS-INPUT
MOVE '1' TO STR(4:1)
END-IF.
IF WS-INPUT = 1
SUBTRACT 1 FROM WS-INPUT
MOVE '1' TO STR(5:1)
END-IF.
IF STR(1:1) IS EQUAL TO "0"
PERFORM NORMAL-ORDER
ELSE
PERFORM REVERSE-ORDER
END-IF.


NORMAL-ORDER.
MOVE 1 TO CC.
IF STR(5:1) = '1'
MOVE "wink," TO WS-RESULT(CC:5)
ADD 5 TO CC
END-IF.
IF STR(4:1) = '1'
MOVE "double blink," TO WS-RESULT(CC:13)
ADD 13 TO CC
END-IF.
IF STR(3:1) = '1'
MOVE "close your eyes," TO WS-RESULT(CC:16)
ADD 16 TO CC
END-IF.
IF STR(2:1) = '1'
MOVE "jump," TO WS-RESULT(CC:5)
ADD 5 TO CC
END-IF.
SUBTRACT 1 FROM CC.
MOVE SPACE TO WS-RESULT(CC:1).


REVERSE-ORDER.
MOVE 1 TO CC.
IF STR(2:1) = '1'
MOVE "jump," TO WS-RESULT(CC:5)
ADD 5 TO CC
END-IF.
IF STR(3:1) = '1'
MOVE "close your eyes," TO WS-RESULT(CC:16)
ADD 16 TO CC
END-IF.
IF STR(4:1) = '1'
MOVE "double blink," TO WS-RESULT(CC:13)
ADD 13 TO CC
END-IF.
IF STR(5:1) = '1'
MOVE "wink," TO WS-RESULT(CC:5)
ADD 5 TO CC
END-IF.
SUBTRACT 1 FROM CC.
MOVE SPACE TO WS-RESULT(CC:1).

63 changes: 63 additions & 0 deletions exercises/practice/secret-handshake/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/secret-handshake/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 4b694b8

Please sign in to comment.