From 598c0f104c96fdb88b2041a17f67850376c0ecfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A1s=20B=20Nagy?= <20251272+BNAndras@users.noreply.github.com> Date: Tue, 4 Jun 2024 22:06:08 -0700 Subject: [PATCH] Add grains --- config.json | 8 +++ .../practice/grains/.docs/instructions.md | 15 +++++ exercises/practice/grains/.meta/config.json | 19 ++++++ .../practice/grains/.meta/src/example.art | 9 +++ exercises/practice/grains/.meta/tests.toml | 43 ++++++++++++++ exercises/practice/grains/src/grains.art | 7 +++ exercises/practice/grains/tester.art | 3 + .../practice/grains/tests/test-grains.art | 59 +++++++++++++++++++ 8 files changed, 163 insertions(+) create mode 100644 exercises/practice/grains/.docs/instructions.md create mode 100644 exercises/practice/grains/.meta/config.json create mode 100644 exercises/practice/grains/.meta/src/example.art create mode 100644 exercises/practice/grains/.meta/tests.toml create mode 100644 exercises/practice/grains/src/grains.art create mode 100644 exercises/practice/grains/tester.art create mode 100644 exercises/practice/grains/tests/test-grains.art diff --git a/config.json b/config.json index 5f952b1..bf49d05 100644 --- a/config.json +++ b/config.json @@ -100,6 +100,14 @@ "prerequisites": [], "difficulty": 2 }, + { + "slug": "grains", + "name": "Grains", + "uuid": "891188ea-1270-450f-a44d-c821047a4344", + "practices": [], + "prerequisites": [], + "difficulty": 2 + }, { "slug": "rna-transcription", "name": "RNA Transcription", 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/config.json b/exercises/practice/grains/.meta/config.json new file mode 100644 index 0000000..507c322 --- /dev/null +++ b/exercises/practice/grains/.meta/config.json @@ -0,0 +1,19 @@ +{ + "authors": [ + "BNAndras" + ], + "files": { + "solution": [ + "src/grains.art" + ], + "test": [ + "tests/test-grains.art" + ], + "example": [ + ".meta/src/example.art" + ] + }, + "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/src/example.art b/exercises/practice/grains/.meta/src/example.art new file mode 100644 index 0000000..1a8deb5 --- /dev/null +++ b/exercises/practice/grains/.meta/src/example.art @@ -0,0 +1,9 @@ +square: function [number][ + switch in? number 1..64 + -> pow 2 sub number 1 + -> null +] + +total: function [][ + sub pow 2 64 1 +] 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/src/grains.art b/exercises/practice/grains/src/grains.art new file mode 100644 index 0000000..694a8c4 --- /dev/null +++ b/exercises/practice/grains/src/grains.art @@ -0,0 +1,7 @@ +square: function [number][ + panic "Please implement the square function" +] + +total: function [][ + panic "Please implement the total function" +] diff --git a/exercises/practice/grains/tester.art b/exercises/practice/grains/tester.art new file mode 100644 index 0000000..80f4a8f --- /dev/null +++ b/exercises/practice/grains/tester.art @@ -0,0 +1,3 @@ +import {unitt}! + +runTests.failFast findTests "tests" diff --git a/exercises/practice/grains/tests/test-grains.art b/exercises/practice/grains/tests/test-grains.art new file mode 100644 index 0000000..d2b53aa --- /dev/null +++ b/exercises/practice/grains/tests/test-grains.art @@ -0,0 +1,59 @@ +import {unitt}! +import {src/grains}! + +suite "Grains" [ + test "grains on square 1" [ + result: square 1 + assert -> 1 = result + ] + + test.skip "grains on square 2" [ + result: square 2 + assert -> 2 = result + ] + + test.skip "grains on square 3" [ + result: square 3 + assert -> 4 = result + ] + + test.skip "grains on square 4" [ + result: square 4 + assert -> 8 = result + ] + + test.skip "grains on square 16" [ + result: square 16 + assert -> 32768 = result + ] + + test.skip "grains on square 32" [ + result: square 32 + assert -> 2147483648 = result + ] + + test.skip "grains on square 64" [ + result: square 64 + assert -> 9223372036854775808 = result + ] + + test.skip "square 0 is invalid" [ + result: square 0 + assert -> null = result + ] + + test.skip "negative square is invalid" [ + result: square neg 1 + assert -> null = result + ] + + test.skip "square greater than 64 is invalid" [ + result: square 65 + assert -> null = result + ] + + test.skip "returns the total number of grains on the board" [ + result: total + assert -> 18446744073709551615 = result + ] +]