Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,14 @@
"prerequisites": [],
"difficulty": 2
},
{
"slug": "grains",
"name": "Grains",
"uuid": "b960441a-2cd9-4384-bfcc-58a1723873da",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "hamming",
"name": "Hamming",
Expand Down
5 changes: 5 additions & 0 deletions exercises/practice/grains/.busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
return {
default = {
ROOT = { '.' }
}
}
11 changes: 11 additions & 0 deletions exercises/practice/grains/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# Instructions

Calculate the number of grains of wheat on a chessboard.

A chessboard has 64 squares.
Square 1 has one grain, square 2 has two grains, square 3 has four grains, and so on, doubling each time.

Write code that calculates:

- the number of grains on a given square
- the total number of grains on the chessboard
6 changes: 6 additions & 0 deletions exercises/practice/grains/.docs/introduction.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Introduction

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 chessboard, with the number of grains doubling on each successive square.
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.moon"
],
"test": [
"grains_spec.moon"
],
"example": [
".meta/example.moon"
]
},
"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://web.archive.org/web/20240908084142/https://coderanch.com/wiki/718824/Grains"
}
8 changes: 8 additions & 0 deletions exercises/practice/grains/.meta/example.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
square: (n) ->
error 'square must be between 1 and 64' unless n >= 1 and n <= 64
2 ^ (n - 1)

total: ->
2 ^ 64 - 1
}
17 changes: 17 additions & 0 deletions exercises/practice/grains/.meta/spec_generator.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
format = (number) ->
string.format '%.0f', number

{
module_name: 'Grains'

generate_test: (case, level) ->
lines = {}
if type(case.expected) == 'table' and case.expected.error
table.insert lines, "assert.has.errors -> Grains.#{case.property} #{case.input.square}, #{quote case.expected.error}"
else if case.property == 'square'
table.insert lines, "assert.are.equal #{format case.expected}, Grains.#{case.property} #{case.input.square}"
else
table.insert lines, "assert.are.equal #{format case.expected}, Grains.#{case.property}!"

table.concat [indent line, level for line in *lines], '\n'
}
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"
7 changes: 7 additions & 0 deletions exercises/practice/grains/grains.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
square: (n) ->
error 'Implement me!'

total: ->
error 'Implement me!'
}
36 changes: 36 additions & 0 deletions exercises/practice/grains/grains_spec.moon
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
Grains = require 'grains'

describe 'grains', ->
describe 'returns the number of grains on the square', ->
it 'grains on square 1', ->
assert.are.equal 1, Grains.square 1

pending 'grains on square 2', ->
assert.are.equal 2, Grains.square 2

pending 'grains on square 3', ->
assert.are.equal 4, Grains.square 3

pending 'grains on square 4', ->
assert.are.equal 8, Grains.square 4

pending 'grains on square 16', ->
assert.are.equal 32768, Grains.square 16

pending 'grains on square 32', ->
assert.are.equal 2147483648, Grains.square 32

pending 'grains on square 64', ->
assert.are.equal 9223372036854775808, Grains.square 64

pending 'square 0 is invalid', ->
assert.has.errors -> Grains.square 0, 'square must be between 1 and 64'

pending 'negative square is invalid', ->
assert.has.errors -> Grains.square -1, 'square must be between 1 and 64'

pending 'square greater than 64 is invalid', ->
assert.has.errors -> Grains.square 65, 'square must be between 1 and 64'

pending 'returns the total number of grains on the board', ->
assert.are.equal 18446744073709551616, Grains.total!
Loading