From 2bbbf8e93b436ce705be9921721f68436adc7faf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ingy=20d=C3=B6t=20Net?= Date: Fri, 13 Sep 2024 21:39:49 -0700 Subject: [PATCH] Implement exercise 'grains' --- config.json | 8 ++ config.yaml | 7 + .../practice/grains/.docs/instructions.md | 15 +++ .../grains/.meta/.yamlscript/exercise.mk | 1 + exercises/practice/grains/.meta/Makefile | 28 ++++ exercises/practice/grains/.meta/config.json | 23 ++++ .../practice/grains/.meta/grains-test.ys | 59 ++++++++ exercises/practice/grains/.meta/grains.ys | 7 + exercises/practice/grains/.meta/tests.toml | 43 ++++++ .../practice/grains/.yamlscript/exercise.mk | 1 + .../grains/.yamlscript/exercism-ys-installer | 127 ++++++++++++++++++ exercises/practice/grains/GNUmakefile | 49 +++++++ exercises/practice/grains/Makefile | 8 ++ exercises/practice/grains/grains-test.ys | 50 +++++++ exercises/practice/grains/grains.ys | 4 + 15 files changed, 430 insertions(+) create mode 100644 exercises/practice/grains/.docs/instructions.md create mode 100644 exercises/practice/grains/.meta/.yamlscript/exercise.mk create mode 100644 exercises/practice/grains/.meta/Makefile create mode 100644 exercises/practice/grains/.meta/config.json create mode 100644 exercises/practice/grains/.meta/grains-test.ys create mode 100644 exercises/practice/grains/.meta/grains.ys create mode 100644 exercises/practice/grains/.meta/tests.toml create mode 100644 exercises/practice/grains/.yamlscript/exercise.mk create mode 100644 exercises/practice/grains/.yamlscript/exercism-ys-installer create mode 100644 exercises/practice/grains/GNUmakefile create mode 100644 exercises/practice/grains/Makefile create mode 100644 exercises/practice/grains/grains-test.ys create mode 100644 exercises/practice/grains/grains.ys diff --git a/config.json b/config.json index b65fe67..02883b1 100644 --- a/config.json +++ b/config.json @@ -403,6 +403,14 @@ "practices": [], "prerequisites": [], "difficulty": 8 + }, + { + "slug": "grains", + "name": "Grains", + "uuid": "53001741-514b-4675-8842-dfd61d45cb28", + "practices": [], + "prerequisites": [], + "difficulty": 1 } ] }, diff --git a/config.yaml b/config.yaml index 3505e84..951d31b 100644 --- a/config.yaml +++ b/config.yaml @@ -366,6 +366,13 @@ exercises: prerequisites: [] difficulty: 8 + - slug: grains + name: Grains + uuid: 53001741-514b-4675-8842-dfd61d45cb28 + practices: [] + prerequisites: [] + difficulty: 1 + tags: - execution_mode/compiled - execution_mode/interpreted diff --git a/exercises/practice/grains/.docs/instructions.md b/exercises/practice/grains/.docs/instructions.md new file mode 100644 index 0000000..df479fc --- /dev/null +++ b/exercises/practice/grains/.docs/instructions.md @@ -0,0 +1,15 @@ +# Instructions + +Calculate the number of grains of wheat on a chessboard given that the number on each square doubles. + +There once was a wise servant who saved the life of a prince. +The king promised to pay whatever the servant could dream up. +Knowing that the king loved chess, the servant told the king he would like to have grains of wheat. +One grain on the first square of a chess board, with the number of grains doubling on each successive square. + +There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on). + +Write code that shows: + +- how many grains were on a given square, and +- the total number of grains on the chessboard diff --git a/exercises/practice/grains/.meta/.yamlscript/exercise.mk b/exercises/practice/grains/.meta/.yamlscript/exercise.mk new file mode 100644 index 0000000..2506034 --- /dev/null +++ b/exercises/practice/grains/.meta/.yamlscript/exercise.mk @@ -0,0 +1 @@ +YS_VERSION := 0.1.75 diff --git a/exercises/practice/grains/.meta/Makefile b/exercises/practice/grains/.meta/Makefile new file mode 100644 index 0000000..ba39335 --- /dev/null +++ b/exercises/practice/grains/.meta/Makefile @@ -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 diff --git a/exercises/practice/grains/.meta/config.json b/exercises/practice/grains/.meta/config.json new file mode 100644 index 0000000..8406cd7 --- /dev/null +++ b/exercises/practice/grains/.meta/config.json @@ -0,0 +1,23 @@ +{ + "authors": [ + "ingydotnet" + ], + "files": { + "solution": [ + "grains.ys" + ], + "test": [ + "grains-test.ys", + "GNUmakefile", + "Makefile", + ".yamlscript/exercise.mk", + ".yamlscript/exercism-ys-installer" + ], + "example": [ + ".meta/grains.ys" + ] + }, + "blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.", + "source": "The CodeRanch Cattle Drive, Assignment 6", + "source_url": "https://coderanch.com/wiki/718824/Grains" +} diff --git a/exercises/practice/grains/.meta/grains-test.ys b/exercises/practice/grains/.meta/grains-test.ys new file mode 100644 index 0000000..3689e14 --- /dev/null +++ b/exercises/practice/grains/.meta/grains-test.ys @@ -0,0 +1,59 @@ +#!/usr/bin/env ys-0 + +require ys::taptest: :all + +use: grains + +# The final tests of square test error conditions +# In these cases you should expect an error as is idiomatic for your language + +test:: +- name: Grains on square 1 + code: square(1) + want: 1 + uuid: 9fbde8de-36b2-49de-baf2-cd42d6f28405 + +- name: Grains on square 2 + code: square(2) + want: 2 + uuid: ee1f30c2-01d8-4298-b25d-c677331b5e6d + +- name: Grains on square 3 + code: square(3) + want: 4 + uuid: 10f45584-2fc3-4875-8ec6-666065d1163b + +- name: Grains on square 4 + code: square(4) + want: 8 + uuid: a7cbe01b-36f4-4601-b053-c5f6ae055170 + +- name: Grains on square 16 + code: square(16) + want: 32768 + uuid: c50acc89-8535-44e4-918f-b848ad2817d4 + +- name: Grains on square 32 + code: square(32) + want: 2147483648 + uuid: acd81b46-c2ad-4951-b848-80d15ed5a04f + +- name: Square 0 is invalid + code: square(0) + what: error + want: square must be between 1 and 64 + uuid: 1d47d832-3e85-4974-9466-5bd35af484e3 + +- name: Negative square is invalid + code: square(-1) + what: error + want: square must be between 1 and 64 + uuid: 61974483-eeb2-465e-be54-ca5dde366453 + +- name: Square greater than 64 is invalid + code: square(65) + what: error + want: square must be between 1 and 64 + uuid: a95e4374-f32c-45a7-a10d-ffec475c012f + +done: 9 diff --git a/exercises/practice/grains/.meta/grains.ys b/exercises/practice/grains/.meta/grains.ys new file mode 100644 index 0000000..0acddb9 --- /dev/null +++ b/exercises/practice/grains/.meta/grains.ys @@ -0,0 +1,7 @@ +!yamlscript/v0 + +defn square(square): + when-not 1 <= square <= 64: + die: 'square must be between 1 and 64' + + 2 **: square.-- diff --git a/exercises/practice/grains/.meta/tests.toml b/exercises/practice/grains/.meta/tests.toml new file mode 100644 index 0000000..6ea68bc --- /dev/null +++ b/exercises/practice/grains/.meta/tests.toml @@ -0,0 +1,43 @@ +# 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. + +[9fbde8de-36b2-49de-baf2-cd42d6f28405] +description = "returns the number of grains on the square -> grains on square 1" + +[ee1f30c2-01d8-4298-b25d-c677331b5e6d] +description = "returns the number of grains on the square -> grains on square 2" + +[10f45584-2fc3-4875-8ec6-666065d1163b] +description = "returns the number of grains on the square -> grains on square 3" + +[a7cbe01b-36f4-4601-b053-c5f6ae055170] +description = "returns the number of grains on the square -> grains on square 4" + +[c50acc89-8535-44e4-918f-b848ad2817d4] +description = "returns the number of grains on the square -> grains on square 16" + +[acd81b46-c2ad-4951-b848-80d15ed5a04f] +description = "returns the number of grains on the square -> grains on square 32" + +[c73b470a-5efb-4d53-9ac6-c5f6487f227b] +description = "returns the number of grains on the square -> grains on square 64" + +[1d47d832-3e85-4974-9466-5bd35af484e3] +description = "returns the number of grains on the square -> square 0 is invalid" + +[61974483-eeb2-465e-be54-ca5dde366453] +description = "returns the number of grains on the square -> negative square is invalid" + +[a95e4374-f32c-45a7-a10d-ffec475c012f] +description = "returns the number of grains on the square -> square greater than 64 is invalid" + +[6eb07385-3659-4b45-a6be-9dc474222750] +description = "returns the total number of grains on the board" diff --git a/exercises/practice/grains/.yamlscript/exercise.mk b/exercises/practice/grains/.yamlscript/exercise.mk new file mode 100644 index 0000000..2506034 --- /dev/null +++ b/exercises/practice/grains/.yamlscript/exercise.mk @@ -0,0 +1 @@ +YS_VERSION := 0.1.75 diff --git a/exercises/practice/grains/.yamlscript/exercism-ys-installer b/exercises/practice/grains/.yamlscript/exercism-ys-installer new file mode 100644 index 0000000..ae3fa01 --- /dev/null +++ b/exercises/practice/grains/.yamlscript/exercism-ys-installer @@ -0,0 +1,127 @@ +#!/env/bin/env bash + +set -euo pipefail + +intro-prompt() ( + cat <<... +-------------------------------------------------------------------------------- + +This YAMLScript Exercism exercise requires the YAMLScript version $version +interpreter command file to be installed here: + + $prefix/bin/ys + +You can install it by pressing Enter now, or by running this command: + + $make install-ys + +This should only take a few seconds and you only need to do this once. +Other exercises will use the same file. + +See https://yamlscript.org/doc/install/ for more YAMLScript installation info. + +-------------------------------------------------------------------------------- + +Would you like to install the 'ys' file now? + +... + + printf "Press Enter to install. Ctl-C to Quit."; read -r +) + +main() { + setup "$@" + + install-from-local + + $auto && intro-prompt + + installed || install-from-release || true + installed || install-from-build || true + installed || + die "Installing '$installed' failed. Giving up." \ + "Consider filing an issue at: $gh_issue_url" + + echo + echo 'Success!' + echo "$installed is now installed." + echo +} + +installed() { + [[ -f $installed ]] +} + +install-from-local() { + local path + path=$(command -v "$ysfq") || true + if [[ -f $path ]]; then + mkdir -p "$bin" + cp "$path" "$bin"/ + ln -fs "$ysfq" "$bin/ys-0" + ln -fs "$ysfq" "$bin/ys" + (installed && $auto) && exit + true + fi +} + +install-from-release() ( + set -x + curl -s https://yamlscript.org/install | + BIN=1 VERSION="$version" PREFIX="$prefix" bash +) + +install-from-build() ( + cat <<... + +The binary release installation failed. +We can attempt to build and install $ysfq now. +This can take from 1 to 5 minutes to complete. + +... + + printf "Press Enter to install. Ctl-C to Quit."; read -r + + [[ -d /tmp && -w /tmp ]] || + die "Can't write to /tmp" \ + 'Giving up.' + + set -x + + rm -fr "$yamlscript_clone" + + git clone --branch="$version" "$yamlscript_repo" "$yamlscript_clone" + + "$make" -C "$yamlscript_clone/ys" install PREFIX="$prefix" +) + +setup() { + version=$1 + prefix=$2 + make=$3 + auto=false + [[ ${4-} ]] && auto=true + + [[ $version =~ ^0\.1\.[0-9]+$ ]] || + die "Invalid YS_VERSION '$version'" + + bin=$prefix/bin + ysfq=ys-$version + installed=$bin/$ysfq + + if installed; then + echo "'$installed' is already installed." + exit + fi + + yamlscript_repo=https://github.com/yaml/yamlscript + yamlscript_clone=/tmp/yamlscript-exercism + gh_issue_url=https://github.com/exercism/yamlscript/issues +} + +die() { + printf '%s\n' "$@" >&2 + exit 1 +} + +main "$@" diff --git a/exercises/practice/grains/GNUmakefile b/exercises/practice/grains/GNUmakefile new file mode 100644 index 0000000..381cd29 --- /dev/null +++ b/exercises/practice/grains/GNUmakefile @@ -0,0 +1,49 @@ +SHELL := bash + +BASE := $(shell pwd) + +export YS_VERSION := 0.1.76 + +YS_LOCAL_PREFIX := ../../../.local/v$(YS_VERSION) +ifeq (,$(shell [[ -d "$(YS_LOCAL_PREFIX)" ]] && echo ok)) +YS_LOCAL_PREFIX := $(shell cd .. && pwd -P)/.local/v$(YS_VERSION) +endif + +YS_LOCAL_BIN := $(YS_LOCAL_PREFIX)/bin +YS_BIN := $(YS_LOCAL_BIN)/ys-$(YS_VERSION) + +YS_INSTALLER := .yamlscript/exercism-ys-installer +YS_INSTALLER_CMD := \ + bash $(YS_INSTALLER) $(YS_VERSION) $(YS_LOCAL_PREFIX) $(MAKE) + +TEST_FILE ?= $(wildcard *-test.ys) + +export PATH := $(YS_LOCAL_BIN):$(PATH) +export YSPATH := $(BASE) + + +#------------------------------------------------------------------------------- +default: + @echo " No default make rule. Try 'make test'." + +test: $(YS_BIN) + prove -v $(TEST_FILE) + +install-ys: + @$(YS_INSTALLER_CMD) + +uninstall-ys: + rm -fr $(YS_LOCAL_PREFIX) + + +#------------------------------------------------------------------------------- +ifdef EXERCISM_YAMLSCRIPT_GHA +$(YS_BIN): + +else ifeq (/mnt/,$(dir $(BASE))) +$(YS_BIN): + +else +$(YS_BIN): + @$(YS_INSTALLER_CMD) auto +endif diff --git a/exercises/practice/grains/Makefile b/exercises/practice/grains/Makefile new file mode 100644 index 0000000..06b6f00 --- /dev/null +++ b/exercises/practice/grains/Makefile @@ -0,0 +1,8 @@ +# This Makefile is a decoy to attempt to detect when a non-GNU make is being +# used and alert the user. + +test: + @echo "You appear to be using a non-GNU version of the 'make' program." + @echo "The YAMLScript Exercism track requires you to use GNU make." + @echo "Please try 'make $@' again using GNU make." + @exit 1 diff --git a/exercises/practice/grains/grains-test.ys b/exercises/practice/grains/grains-test.ys new file mode 100644 index 0000000..257d40a --- /dev/null +++ b/exercises/practice/grains/grains-test.ys @@ -0,0 +1,50 @@ +#!/usr/bin/env ys-0 + +require ys::taptest: :all + +use: grains + +# The final tests of square test error conditions +# In these cases you should expect an error as is idiomatic for your language + +test:: +- name: Grains on square 1 + code: square(1) + want: 1 + +- name: Grains on square 2 + code: square(2) + want: 2 + +- name: Grains on square 3 + code: square(3) + want: 4 + +- name: Grains on square 4 + code: square(4) + want: 8 + +- name: Grains on square 16 + code: square(16) + want: 32768 + +- name: Grains on square 32 + code: square(32) + want: 2147483648 + +- name: Square 0 is invalid + code: square(0) + what: error + want: square must be between 1 and 64 + +- name: Negative square is invalid + code: square(-1) + what: error + want: square must be between 1 and 64 + +- name: Square greater than 64 is invalid + code: square(65) + what: error + want: square must be between 1 and 64 + +done: 9 diff --git a/exercises/practice/grains/grains.ys b/exercises/practice/grains/grains.ys new file mode 100644 index 0000000..fa3e987 --- /dev/null +++ b/exercises/practice/grains/grains.ys @@ -0,0 +1,4 @@ +!yamlscript/v0 + +defn square(square): + # Implement the 'square' function.