-
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
467 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
] | ||
|
Oops, something went wrong.