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 20, 2023
1 parent 8881110 commit 150fa23
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 0 deletions.
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,14 @@
"strings"
]
},
{
"slug": "grains",
"name": "Grains",
"uuid": "99408193-d0ce-4c68-8cab-e680f3ed56a4",
"practices": [],
"prerequisites": [],
"difficulty": 2
},
{
"slug": "isogram",
"name": "Isogram",
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.vim"
],
"test": [
"grains.vader"
],
"example": [
".meta/example.vim"
]
},
"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"
}
11 changes: 11 additions & 0 deletions exercises/practice/grains/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function! Square(number) abort
if a:number < 1 || a:number > 64
throw 'square must be between 1 and 64'
endif

return float2nr(pow(2, (a:number-1)))
endfunction

function! Total() abort
return float2nr(pow(2, 64) - 1)
endfunction
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"
57 changes: 57 additions & 0 deletions exercises/practice/grains/grains.vader
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@

Execute (grains on square 1):
let g:square = 1
let g:expected = 1
AssertEqual g:expected, Square(g:square)

Execute (grains on square 2):
let g:square = 2
let g:expected = 2
AssertEqual g:expected, Square(g:square)

Execute (grains on square 3):
let g:square = 3
let g:expected = 4
AssertEqual g:expected, Square(g:square)

Execute (grains on square 4):
let g:square = 4
let g:expected = 8
AssertEqual g:expected, Square(g:square)

Execute (grains on square 16):
let g:square = 16
let g:expected = 32768
AssertEqual g:expected, Square(g:square)

Execute (grains on square 32):
let g:square = 32
let g:expected = 2147483648
AssertEqual g:expected, Square(g:square)

Execute (grains on square 64):
let g:square = 64
let g:expected = 9223372036854775807
AssertEqual g:expected, Square(g:square)

Execute (square 0 is invalid):
let g:square = 0
let g:expected = "square must be between 1 and 64"
AssertThrows call Square(g:square)
AssertEqual g:expected, g:vader_exception

Execute (negative square is invalid):
let g:square = -1
let g:expected = "square must be between 1 and 64"
AssertThrows call Square(g:square)
AssertEqual g:expected, g:vader_exception

Execute (square greater than 64 is invalid):
let g:square = 65
let g:expected = "square must be between 1 and 64"
AssertThrows call Square(g:square)
AssertEqual g:expected, g:vader_exception

Execute (returns the total number of grains on the board):
let g:expected = 9223372036854775807
AssertEqual g:expected, Total()
23 changes: 23 additions & 0 deletions exercises/practice/grains/grains.vim
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"
" Returns the number of grains on a chessboard square given the grains on each square
" double from the previous square.
" Throws an error if the square is below 1 or above 64.
"
" Examples:
"
" :echo Square(16)
" 32768
"
" :echo Square(-1)
" E605: Exception not caught: square must be between 1 and 64
"
function! Square(number) abort
" your code goes here
endfunction

"
" Returns the total number of grains for a filled chessboard
"
function! Total(number) abort
" your code goes here
endfunction

0 comments on commit 150fa23

Please sign in to comment.