Skip to content

Commit ae347bb

Browse files
authored
add grains (#97)
* add `grains` * Fix missng double quotes
1 parent d5f36cd commit ae347bb

10 files changed

Lines changed: 160 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
"prerequisites": [],
9999
"difficulty": 2
100100
},
101+
{
102+
"slug": "grains",
103+
"name": "Grains",
104+
"uuid": "b960441a-2cd9-4384-bfcc-58a1723873da",
105+
"practices": [],
106+
"prerequisites": [],
107+
"difficulty": 2
108+
},
101109
{
102110
"slug": "hamming",
103111
"name": "Hamming",

exercises/practice/grains/.busted

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
return {
2+
default = {
3+
ROOT = { '.' }
4+
}
5+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Instructions
2+
3+
Calculate the number of grains of wheat on a chessboard.
4+
5+
A chessboard has 64 squares.
6+
Square 1 has one grain, square 2 has two grains, square 3 has four grains, and so on, doubling each time.
7+
8+
Write code that calculates:
9+
10+
- the number of grains on a given square
11+
- the total number of grains on the chessboard
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Introduction
2+
3+
There once was a wise servant who saved the life of a prince.
4+
The king promised to pay whatever the servant could dream up.
5+
Knowing that the king loved chess, the servant told the king he would like to have grains of wheat.
6+
One grain on the first square of a chessboard, with the number of grains doubling on each successive square.
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"authors": [
3+
"BNAndras"
4+
],
5+
"files": {
6+
"solution": [
7+
"grains.moon"
8+
],
9+
"test": [
10+
"grains_spec.moon"
11+
],
12+
"example": [
13+
".meta/example.moon"
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://web.archive.org/web/20240908084142/https://coderanch.com/wiki/718824/Grains"
19+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
square: (n) ->
3+
error 'square must be between 1 and 64' unless n >= 1 and n <= 64
4+
2 ^ (n - 1)
5+
6+
total: ->
7+
2 ^ 64 - 1
8+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
format = (number) ->
2+
string.format '%.0f', number
3+
4+
{
5+
module_name: 'Grains'
6+
7+
generate_test: (case, level) ->
8+
lines = {}
9+
if type(case.expected) == 'table' and case.expected.error
10+
table.insert lines, "assert.has.errors -> Grains.#{case.property} #{case.input.square}, #{quote case.expected.error}"
11+
else if case.property == 'square'
12+
table.insert lines, "assert.are.equal #{format case.expected}, Grains.#{case.property} #{case.input.square}"
13+
else
14+
table.insert lines, "assert.are.equal #{format case.expected}, Grains.#{case.property}!"
15+
16+
table.concat [indent line, level for line in *lines], '\n'
17+
}
Lines changed: 43 additions & 0 deletions
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"
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
square: (n) ->
3+
error 'Implement me!'
4+
5+
total: ->
6+
error 'Implement me!'
7+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
Grains = require 'grains'
2+
3+
describe 'grains', ->
4+
describe 'returns the number of grains on the square', ->
5+
it 'grains on square 1', ->
6+
assert.are.equal 1, Grains.square 1
7+
8+
pending 'grains on square 2', ->
9+
assert.are.equal 2, Grains.square 2
10+
11+
pending 'grains on square 3', ->
12+
assert.are.equal 4, Grains.square 3
13+
14+
pending 'grains on square 4', ->
15+
assert.are.equal 8, Grains.square 4
16+
17+
pending 'grains on square 16', ->
18+
assert.are.equal 32768, Grains.square 16
19+
20+
pending 'grains on square 32', ->
21+
assert.are.equal 2147483648, Grains.square 32
22+
23+
pending 'grains on square 64', ->
24+
assert.are.equal 9223372036854775808, Grains.square 64
25+
26+
pending 'square 0 is invalid', ->
27+
assert.has.errors -> Grains.square 0, 'square must be between 1 and 64'
28+
29+
pending 'negative square is invalid', ->
30+
assert.has.errors -> Grains.square -1, 'square must be between 1 and 64'
31+
32+
pending 'square greater than 64 is invalid', ->
33+
assert.has.errors -> Grains.square 65, 'square must be between 1 and 64'
34+
35+
pending 'returns the total number of grains on the board', ->
36+
assert.are.equal 18446744073709551616, Grains.total!

0 commit comments

Comments
 (0)