Skip to content

Commit

Permalink
Add grains exercise
Browse files Browse the repository at this point in the history
  • Loading branch information
BNAndras committed Nov 23, 2023
1 parent 78a3fd9 commit ec1a389
Show file tree
Hide file tree
Showing 8 changed files with 467 additions and 0 deletions.
11 changes: 11 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,17 @@
"blocks"
],
"difficulty": 9
},
{
"slug": "grains",
"name": "Grains",
"uuid": "668915db-bc3d-4ff4-a339-174ed2f35940",
"practices": [
"errors",
"loops"
],
"prerequisites": [],
"difficulty": 3
}
]
}
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
19 changes: 19 additions & 0 deletions exercises/practice/grains/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"authors": [
"BNAndras"
],
"files": {
"solution": [
"grains.red"
],
"test": [
"grains-test.red"
],
"example": [
".meta/example.red"
]
},
"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"
}
24 changes: 24 additions & 0 deletions exercises/practice/grains/.meta/example.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
Red [
description: {"Grains" exercise solution for exercism platform}
author: "BNAndras"
]

square: function [
"Returns the number of grains on a given square"
square [integer!]
return: [integer!]
] [
if (square < 1) or (square > 64) [
cause-error 'user 'message "square must be between 1 and 64"
]

power 2 (square - 1)
]

total: function [
"Returns the total number of grains on a chessboard"
return: [integer!]
] [
(power 2 64) - 1
]

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"
123 changes: 123 additions & 0 deletions exercises/practice/grains/grains-test.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
Red [
description: {Tests for "Grains" Exercism exercise}
author: "loziniak"
]

#include %testlib.red

test-init/limit %grains.red 1
; test-init/limit %.meta/example.red 1 ; test example solution

canonical-cases: [#(
description: "grains on square 1"
input: #(
square: 1
)
expected: 1
function: "square"
uuid: "9fbde8de-36b2-49de-baf2-cd42d6f28405"
) #(
description: "grains on square 2"
input: #(
square: 2
)
expected: 2
function: "square"
uuid: "ee1f30c2-01d8-4298-b25d-c677331b5e6d"
) #(
description: "grains on square 3"
input: #(
square: 3
)
expected: 4
function: "square"
uuid: "10f45584-2fc3-4875-8ec6-666065d1163b"
) #(
description: "grains on square 4"
input: #(
square: 4
)
expected: 8
function: "square"
uuid: "a7cbe01b-36f4-4601-b053-c5f6ae055170"
) #(
description: "grains on square 16"
input: #(
square: 16
)
expected: 32768
function: "square"
uuid: "c50acc89-8535-44e4-918f-b848ad2817d4"
) #(
description: "grains on square 32"
input: #(
square: 32
)
expected: 2147483648.0
function: "square"
uuid: "acd81b46-c2ad-4951-b848-80d15ed5a04f"
) #(
description: "grains on square 64"
input: #(
square: 64
)
expected: 9.223372036854776e18
function: "square"
uuid: "c73b470a-5efb-4d53-9ac6-c5f6487f227b"
) #(
description: "square 0 is invalid"
input: #(
square: 0
)
expected: #(
error: "square must be between 1 and 64"
)
function: "square"
uuid: "1d47d832-3e85-4974-9466-5bd35af484e3"
) #(
description: "negative square is invalid"
input: #(
square: -1
)
expected: #(
error: "square must be between 1 and 64"
)
function: "square"
uuid: "61974483-eeb2-465e-be54-ca5dde366453"
) #(
description: "square greater than 64 is invalid"
input: #(
square: 65
)
expected: #(
error: "square must be between 1 and 64"
)
function: "square"
uuid: "a95e4374-f32c-45a7-a10d-ffec475c012f"
) #(
description: "returns the total number of grains on the board"
input: #()
expected: 18446744073709551615
function: "total"
uuid: "6eb07385-3659-4b45-a6be-9dc474222750"
)]


foreach c-case canonical-cases [
expect-code: compose [
(to word! c-case/function) (values-of c-case/input)
]
case-code: reduce
either all [
map? c-case/expected
string? c-case/expected/error
] [
['expect-error/message quote 'user expect-code c-case/expected/error]
] [
['expect c-case/expected expect-code]
]

test c-case/description case-code
]

test-results/print
15 changes: 15 additions & 0 deletions exercises/practice/grains/grains.red
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
Red [
description: {"Grains" exercise solution for exercism platform}
author: "" ; you can write your name here, in quotes
]

square: function [
square
] [
cause-error 'user 'message "You need to implement square function."
]

total: function [] [
cause-error 'user 'message "You need to implement total function."
]

Loading

0 comments on commit ec1a389

Please sign in to comment.