Skip to content

Commit 6933be3

Browse files
committed
Add grains exercise
1 parent 0431038 commit 6933be3

File tree

7 files changed

+143
-0
lines changed

7 files changed

+143
-0
lines changed

config.json

+8
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,14 @@
139139
"prerequisites": [],
140140
"difficulty": 2
141141
},
142+
{
143+
"slug": "grains",
144+
"name": "Grains",
145+
"uuid": "e163b0a9-b9fe-4303-b047-11e588d684da",
146+
"practices": [],
147+
"prerequisites": [],
148+
"difficulty": 2
149+
},
142150
{
143151
"slug": "luhn",
144152
"name": "Luhn",
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Instructions
2+
3+
Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.
4+
5+
There once was a wise servant who saved the life of a prince.
6+
The king promised to pay whatever the servant could dream up.
7+
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
8+
One grain on the first square of a chess board, with the number of grains doubling on each successive square.
9+
10+
There are 64 squares on a chessboard (where square 1 has one grain, square 2 has two grains, and so on).
11+
12+
Write code that shows:
13+
14+
- how many grains were on a given square, and
15+
- the total number of grains on the chessboard
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"pfertyk"
4+
],
5+
"files": {
6+
"solution": [
7+
"grains.gd"
8+
],
9+
"test": [
10+
"grains_test.gd"
11+
],
12+
"example": [
13+
".meta/example.gd"
14+
]
15+
},
16+
"blurb": "Calculate the number of grains of wheat on a chessboard given that the number on each square doubles.",
17+
"source": "The CodeRanch Cattle Drive, Assignment 6",
18+
"source_url": "https://coderanch.com/wiki/718824/Grains"
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
func square(number):
2+
if number < 1 or number > 64:
3+
return null
4+
return pow(2, number - 1)
5+
6+
7+
func total():
8+
return pow(2, 64) - 1
+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# This is an auto-generated file.
2+
#
3+
# Regenerating this file via `configlet sync` will:
4+
# - Recreate every `description` key/value pair
5+
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications
6+
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion)
7+
# - Preserve any other key/value pair
8+
#
9+
# As user-added comments (using the # character) will be removed when this file
10+
# is regenerated, comments can be added via a `comment` key.
11+
12+
[9fbde8de-36b2-49de-baf2-cd42d6f28405]
13+
description = "returns the number of grains on the square -> grains on square 1"
14+
15+
[ee1f30c2-01d8-4298-b25d-c677331b5e6d]
16+
description = "returns the number of grains on the square -> grains on square 2"
17+
18+
[10f45584-2fc3-4875-8ec6-666065d1163b]
19+
description = "returns the number of grains on the square -> grains on square 3"
20+
21+
[a7cbe01b-36f4-4601-b053-c5f6ae055170]
22+
description = "returns the number of grains on the square -> grains on square 4"
23+
24+
[c50acc89-8535-44e4-918f-b848ad2817d4]
25+
description = "returns the number of grains on the square -> grains on square 16"
26+
27+
[acd81b46-c2ad-4951-b848-80d15ed5a04f]
28+
description = "returns the number of grains on the square -> grains on square 32"
29+
30+
[c73b470a-5efb-4d53-9ac6-c5f6487f227b]
31+
description = "returns the number of grains on the square -> grains on square 64"
32+
33+
[1d47d832-3e85-4974-9466-5bd35af484e3]
34+
description = "returns the number of grains on the square -> square 0 is invalid"
35+
36+
[61974483-eeb2-465e-be54-ca5dde366453]
37+
description = "returns the number of grains on the square -> negative square is invalid"
38+
39+
[a95e4374-f32c-45a7-a10d-ffec475c012f]
40+
description = "returns the number of grains on the square -> square greater than 64 is invalid"
41+
42+
[6eb07385-3659-4b45-a6be-9dc474222750]
43+
description = "returns the total number of grains on the board"

exercises/practice/grains/grains.gd

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
func square(number):
2+
pass
3+
4+
5+
func total():
6+
pass
+44
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
func test_grains_on_square_1(solution_script):
2+
return [solution_script.square(1), 1]
3+
4+
5+
func test_grains_on_square_2(solution_script):
6+
return [solution_script.square(2), 2]
7+
8+
9+
func test_grains_on_square_3(solution_script):
10+
return [solution_script.square(3), 4]
11+
12+
13+
func test_grains_on_square_4(solution_script):
14+
return [solution_script.square(4), 8]
15+
16+
17+
func test_grains_on_square_16(solution_script):
18+
return [solution_script.square(16), 32768]
19+
20+
21+
func test_grains_on_square_32(solution_script):
22+
return [solution_script.square(32), 2147483648]
23+
24+
25+
func test_grains_on_square_64(solution_script):
26+
return [solution_script.square(64), 9223372036854775808]
27+
28+
29+
func test_square_0_is_invalid(solution_script):
30+
return [solution_script.square(0), null]
31+
32+
33+
func test_negative_square_is_invalid(solution_script):
34+
return [solution_script.square(-1), null]
35+
36+
37+
func test_square_greater_than_64_is_invalid(solution_script):
38+
return [solution_script.square(65), null]
39+
40+
41+
func test_returns_the_total_number_of_grains_on_the_board(solution_script):
42+
return [solution_script.total(), 18446744073709551615]
43+
44+

0 commit comments

Comments
 (0)