diff --git a/config.json b/config.json index 6d40e34d..ce1e6d55 100644 --- a/config.json +++ b/config.json @@ -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", diff --git a/exercises/practice/secret-handshake/.docs/instructions.md b/exercises/practice/secret-handshake/.docs/instructions.md new file mode 100644 index 00000000..d4d57b80 --- /dev/null +++ b/exercises/practice/secret-handshake/.docs/instructions.md @@ -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"]. diff --git a/exercises/practice/secret-handshake/.docs/introduction.md b/exercises/practice/secret-handshake/.docs/introduction.md new file mode 100644 index 00000000..450e6092 --- /dev/null +++ b/exercises/practice/secret-handshake/.docs/introduction.md @@ -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"]. diff --git a/exercises/practice/secret-handshake/.meta/config.json b/exercises/practice/secret-handshake/.meta/config.json new file mode 100644 index 00000000..ddc680b8 --- /dev/null +++ b/exercises/practice/secret-handshake/.meta/config.json @@ -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" +} diff --git a/exercises/practice/secret-handshake/.meta/proof.ci.cob b/exercises/practice/secret-handshake/.meta/proof.ci.cob new file mode 100644 index 00000000..ab399f55 --- /dev/null +++ b/exercises/practice/secret-handshake/.meta/proof.ci.cob @@ -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). + diff --git a/exercises/practice/secret-handshake/bin/fetch-cobolcheck b/exercises/practice/secret-handshake/bin/fetch-cobolcheck new file mode 100755 index 00000000..64230fe2 --- /dev/null +++ b/exercises/practice/secret-handshake/bin/fetch-cobolcheck @@ -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 diff --git a/exercises/practice/secret-handshake/bin/fetch-cobolcheck.ps1 b/exercises/practice/secret-handshake/bin/fetch-cobolcheck.ps1 new file mode 100644 index 00000000..261f5f45 --- /dev/null +++ b/exercises/practice/secret-handshake/bin/fetch-cobolcheck.ps1 @@ -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 \ No newline at end of file diff --git a/exercises/practice/secret-handshake/config.properties b/exercises/practice/secret-handshake/config.properties new file mode 100644 index 00000000..2c5a6c8e --- /dev/null +++ b/exercises/practice/secret-handshake/config.properties @@ -0,0 +1,183 @@ +# Configuration settings for Cobol Check + +#--------------------------------------------------------------------------------------------------------------------- +# This configuration - echoed to console when Cobol Check is executed, for information only. +#--------------------------------------------------------------------------------------------------------------------- +config.loaded = production + +#--------------------------------------------------------------------------------------------------------------------- +# Prefix for field names and paragraph names in the test management code that cobol-check +# inserts into programs to be tested. The default is "UT-". If this conflicts with names +# in the programs to be tested, you can override it with a value you specify here. +# The value must be 3 characters or less. Cannot be empty. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.prefix = UT- + +#--------------------------------------------------------------------------------------------------------------------- +# Tags written in the generated test code in the form of a comment, when a code injection starts and ends. +# Default is null, which will prevent the tags from appearing. Any other value will appear as comments +# surrounding the injected code. +# Examples: +# cobolcheck.injectedCodeTag.start = ###INJECT START### +# cobolcheck.injectedCodeTag.end = ###INJECT END### +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.injectedCodeTag.start = null +cobolcheck.injectedCodeTag.end = null + +#--------------------------------------------------------------------------------------------------------------------- +# A tag written at the start of entities stubbed by default. Recommended value-length <= 4. +# Note: The tag will appear only when cobolcheck stubs lines by default. +# This is the case for CALLs and batch file IO verbs. +# Default is null, which will prevent the tag from appearing. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.stub.comment.tag = null + +#--------------------------------------------------------------------------------------------------------------------- +# Determines if cobolcheck should generate code, such that decimal point is comma. +# The default is "false". The value should be set to "true" if the compiler is set to +# read decimal points as commas. If the cobol source program sets DECIMAL-POINT IS COMMA, +# this configuration will be overwritten. +# Example: 1,385,481.00 (decimalPointIsComma = false) +# Example: 1.385.481,00 (decimalPointIsComma = true) +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.decimalPointIsComma = false + +#--------------------------------------------------------------------------------------------------------------------- +# If the source program contains rules as the first line follwed by CBL, the given value will be appended +# to this. +# If no CBL is found in source, it will be added along with the given value +# default is null, which will make no changes. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.append.rules = null + +#--------------------------------------------------------------------------------------------------------------------- +# Path for the generated Cobol test code +# Default: ./ +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.program.path = ./ + +#--------------------------------------------------------------------------------------------------------------------- +# Suffix to append to the name of each program under test to produce the name of the corresponding +# test program that contains the merged test code. +# Example: For program ABCXYZ4, if suffix is T.CBL then the test program name will be ABCXYZ4T.CBL. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.program.name = test.cob + +#--------------------------------------------------------------------------------------------------------------------- +# Path for the generated testsuite parse error log +# Default: ./ +#--------------------------------------------------------------------------------------------------------------------- +testsuite.parser.error.log.path = ./ + +#--------------------------------------------------------------------------------------------------------------------- +# Name of the generated testsuite parse error log file - with extension +# Default: ParserErrorLog.txt +#--------------------------------------------------------------------------------------------------------------------- +testsuite.parser.error.log.name = ParserErrorLog.txt + +#--------------------------------------------------------------------------------------------------------------------- +# The charset that cobolcheck will use when reading- and writing to files. +# See https://docs.oracle.com/javase/8/docs/technotes/guides/intl/encoding.doc.html, for a list of +# valid values. +# Default value for each OS is , which will use the default encoding for the OS. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.file.encoding.linux = default +cobolcheck.file.encoding.macosx = default +cobolcheck.file.encoding.windows = default +cobolcheck.file.encoding.zos = default +cobolcheck.file.encoding.unix = default + +#--------------------------------------------------------------------------------------------------------------------- +# Sets permissions for all files generated by Cobol Check, for all users. +# If read, write and execute permissions are set, all users can perform said actions on all files +# that Cobol Check generates. +# Value can be any permutation of the letters: 'rwx' (read, write, execute) or none - meaning no permissions. +# Default value: rx +#--------------------------------------------------------------------------------------------------------------------- +generated.files.permission.all = rx + +#--------------------------------------------------------------------------------------------------------------------- +# Determines if Cobol Check should run the generated test program. +# Default is true. +# If set to false, Cobol Check will generate the code, but not run it. If more than one program +# is given as a command line option, the generated test file will be overwritten. Thus if set to false, +# only one program should be given at a time. +#--------------------------------------------------------------------------------------------------------------------- +cobolcheck.test.run = false + +#--------------------------------------------------------------------------------------------------------------------- +# These settings are to locate the application code under test in *your* Cobol project directory tree. +# Can be absolute path or relative to project root. +#--------------------------------------------------------------------------------------------------------------------- +application.source.directory = src +application.copybook.directory = cpy + +#--------------------------------------------------------------------------------------------------------------------- +# Location of test suite input file(s). This can also be passed on command-line option --test-suite-path. +#--------------------------------------------------------------------------------------------------------------------- +test.suite.directory = tst + +#--------------------------------------------------------------------------------------------------------------------- +# Location of test output. File extension is determined by a given format. +#--------------------------------------------------------------------------------------------------------------------- +test.results.file = output/testResults + +#--------------------------------------------------------------------------------------------------------------------- +# Determines the format of the test results written to the output file. +# Supported formats: txt, xml, html. +#--------------------------------------------------------------------------------------------------------------------- +test.results.format = txt + +#--------------------------------------------------------------------------------------------------------------------- +# Determines the format style of the test results written to the output file. +# The style controls the hierarchy and structure of data and naming of the 'object' that is written +# in a given format. Format: txt and style: directOutput are exclusive. txt cannot use any other style +# than directOutput, and directOutput cannot be used with any other format than txt. +# Other formats and styles can be used interchangeably. +# Supported styles: directOutput, JUnit, tableDocument, tableEmbed +#--------------------------------------------------------------------------------------------------------------------- +test.results.format.style = directOutput + +#--------------------------------------------------------------------------------------------------------------------- +# If application source filenames have a suffix, specify it here without the period or dot. +#--------------------------------------------------------------------------------------------------------------------- +application.source.filename.suffix = CBL,cbl,COB,cob + +#--------------------------------------------------------------------------------------------------------------------- +# If application copybook filenames have a suffix, specify it here without the period or dot +# e.g. application.copybook.filename.suffix = CBL +#--------------------------------------------------------------------------------------------------------------------- +application.copybook.filename.suffix = CBL,cbl,COB,cob + +#--------------------------------------------------------------------------------------------------------------------- +# Optional override of system default Locale for log messages and exception messages. +#--------------------------------------------------------------------------------------------------------------------- +#locale.language = ja +#locale.country = +#locale.variant = + +#--------------------------------------------------------------------------------------------------------------------- +# Cobol Check concatenates multiple test suite input files into a single file for the Generator. +# This is the relative or absolute path of the concatenated file. If not specified, the default +# is "./ALLTESTS" relative to the directory in which Cobol Check was started. +#--------------------------------------------------------------------------------------------------------------------- +concatenated.test.suites = ./ALLTESTS + +#--------------------------------------------------------------------------------------------------------------------- +# Shell scripts and JCL files for executing your test suites. +# Cobol Check will invoke one of these after creating the copy of the program under test that contains +# test code generated from your test suites. +# Unix and Mac OS X are both treated as unix. Most unices can run the linux script. +# Unix is the default. +# You can write custom scripts/JCL for your environment, for instance if you are using a different Cobol compiler. +# The first element in these names reflects the first few characters returned by Java's System.getProperty("os.name"). +# Cobol Check will select one of these entries based on which platform it thinks it's running on. +#--------------------------------------------------------------------------------------------------------------------- + +cobolcheck.script.directory = scripts +linux.process = linux_gnucobol_run_tests +osx.process = linux_gnucobol_run_tests +freebsd.process = linux_gnucobol_run_tests +windows.process = windows_gnucobol_run_tests.cmd +zos.process = +unix.process = linux_gnucobol_run_tests diff --git a/exercises/practice/secret-handshake/src/secret-handshake.cob b/exercises/practice/secret-handshake/src/secret-handshake.cob new file mode 100644 index 00000000..d02cb6ed --- /dev/null +++ b/exercises/practice/secret-handshake/src/secret-handshake.cob @@ -0,0 +1,11 @@ + IDENTIFICATION DIVISION. + PROGRAM-ID. SECRET-HANDSHAKE. + ENVIRONMENT DIVISION. + DATA DIVISION. + WORKING-STORAGE SECTION. + 01 WS-INPUT PIC 999. + 01 WS-RESULT PIC X(60). + + PROCEDURE DIVISION. + + COMMANDS. \ No newline at end of file diff --git a/exercises/practice/secret-handshake/test.ps1 b/exercises/practice/secret-handshake/test.ps1 new file mode 100644 index 00000000..ccfa530a --- /dev/null +++ b/exercises/practice/secret-handshake/test.ps1 @@ -0,0 +1,16 @@ +$slug=Split-Path $PSScriptRoot -Leaf + +if (![System.IO.File]::Exists("$PSScriptRoot\bin\cobolcheck.exe")){ + Write-Output "Cobolcheck not found. Trying to fetch it." + & "$PSScriptRoot\bin\fetch-cobolcheck.ps1" +} + +Write-Output "Run cobolcheck." +Set-Location $PSScriptRoot + +Invoke-Expression "bin\cobolcheck.exe -p $slug" +Invoke-Expression "cobc -xj test.cob" + +if ($Lastexitcode -ne 0) { + exit $Lastexitcode +} diff --git a/exercises/practice/secret-handshake/test.sh b/exercises/practice/secret-handshake/test.sh new file mode 100755 index 00000000..ed4a27a1 --- /dev/null +++ b/exercises/practice/secret-handshake/test.sh @@ -0,0 +1,20 @@ +#!/usr/bin/env bash +SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )/ +SLUG=${1:-$(basename "${SCRIPT_DIR}")} +COBOLCHECK=./bin/cobolcheck + +WHICH_COBOLCHECK=$(which cobolcheck) +if [[ $? -eq 0 ]] ; then + echo "Found cobolcheck, using $COBOLCHECK" + COBOLCHECK=$WHICH_COBOLCHECK +elif [ ! -f $SCRIPT_DIR/bin/cobolcheck ]; then + echo "Cobolcheck not found, try to fetch it." + cd $SCRIPT_DIR/bin/ + bash fetch-cobolcheck +fi +cd $SCRIPT_DIR +$COBOLCHECK -p $SLUG + +# compile and run +echo "COMPILE AND RUN TEST" +cobc -xj test.cob diff --git a/exercises/practice/secret-handshake/tst/secret-handshake/secret-handshake.cut b/exercises/practice/secret-handshake/tst/secret-handshake/secret-handshake.cut new file mode 100644 index 00000000..29542192 --- /dev/null +++ b/exercises/practice/secret-handshake/tst/secret-handshake/secret-handshake.cut @@ -0,0 +1,55 @@ +TestCase "wink for 1" + MOVE 1 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "wink" + +TestCase "double blink for 10" + MOVE 2 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "double blink" + +TestCase "close your eyes for 100" + MOVE 4 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "close your eyes" + +TestCase "jump for 1000" + MOVE 8 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "jump" + +TestCase "combine two actions" + MOVE 3 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "wink,double blink" + +TestCase "reverse two actions" + MOVE 19 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "double blink,wink" + +TestCase "reversing one action gives the same action" + MOVE 24 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "jump" + +TestCase "reversing no actions still gives no actions" + MOVE 16 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = " " + +TestCase "all possible actions" + MOVE 15 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "wink,double blink,close your eyes,jump" + +TestCase "reverse all possible actions" + MOVE 31 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = "jump,close your eyes,double blink,wink" + +TestCase "do nothing for zero" + MOVE 0 TO WS-INPUT + PERFORM COMMANDS + EXPECT WS-RESULT = " " +