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 'grains' #27

Merged
merged 1 commit into from
Sep 17, 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 @@ -403,6 +403,14 @@
"practices": [],
"prerequisites": [],
"difficulty": 8
},
{
"slug": "grains",
"name": "Grains",
"uuid": "53001741-514b-4675-8842-dfd61d45cb28",
"practices": [],
"prerequisites": [],
"difficulty": 1
}
]
},
Expand Down
7 changes: 7 additions & 0 deletions config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions exercises/practice/grains/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions exercises/practice/grains/.meta/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
28 changes: 28 additions & 0 deletions exercises/practice/grains/.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/grains/.meta/config.json
Original file line number Diff line number Diff line change
@@ -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"
}
59 changes: 59 additions & 0 deletions exercises/practice/grains/.meta/grains-test.ys
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions exercises/practice/grains/.meta/grains.ys
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
!yamlscript/v0

defn square(square):
when-not 1 <= square <= 64:
die: 'square must be between 1 and 64'

2 **: square.--
43 changes: 43 additions & 0 deletions exercises/practice/grains/.meta/tests.toml
Original file line number Diff line number Diff line change
@@ -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"
1 change: 1 addition & 0 deletions exercises/practice/grains/.yamlscript/exercise.mk
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
YS_VERSION := 0.1.75
127 changes: 127 additions & 0 deletions exercises/practice/grains/.yamlscript/exercism-ys-installer
Original file line number Diff line number Diff line change
@@ -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 "$@"
49 changes: 49 additions & 0 deletions exercises/practice/grains/GNUmakefile
Original file line number Diff line number Diff line change
@@ -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
Loading