Skip to content

Commit 150fa23

Browse files
committed
Add grains exercise
1 parent 8881110 commit 150fa23

File tree

7 files changed

+176
-0
lines changed

7 files changed

+176
-0
lines changed

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,14 @@
158158
"strings"
159159
]
160160
},
161+
{
162+
"slug": "grains",
163+
"name": "Grains",
164+
"uuid": "99408193-d0ce-4c68-8cab-e680f3ed56a4",
165+
"practices": [],
166+
"prerequisites": [],
167+
"difficulty": 2
168+
},
161169
{
162170
"slug": "isogram",
163171
"name": "Isogram",
Lines changed: 15 additions & 0 deletions
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
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.vim"
8+
],
9+
"test": [
10+
"grains.vader"
11+
],
12+
"example": [
13+
".meta/example.vim"
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
function! Square(number) abort
2+
if a:number < 1 || a:number > 64
3+
throw 'square must be between 1 and 64'
4+
endif
5+
6+
return float2nr(pow(2, (a:number-1)))
7+
endfunction
8+
9+
function! Total() abort
10+
return float2nr(pow(2, 64) - 1)
11+
endfunction
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: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
2+
Execute (grains on square 1):
3+
let g:square = 1
4+
let g:expected = 1
5+
AssertEqual g:expected, Square(g:square)
6+
7+
Execute (grains on square 2):
8+
let g:square = 2
9+
let g:expected = 2
10+
AssertEqual g:expected, Square(g:square)
11+
12+
Execute (grains on square 3):
13+
let g:square = 3
14+
let g:expected = 4
15+
AssertEqual g:expected, Square(g:square)
16+
17+
Execute (grains on square 4):
18+
let g:square = 4
19+
let g:expected = 8
20+
AssertEqual g:expected, Square(g:square)
21+
22+
Execute (grains on square 16):
23+
let g:square = 16
24+
let g:expected = 32768
25+
AssertEqual g:expected, Square(g:square)
26+
27+
Execute (grains on square 32):
28+
let g:square = 32
29+
let g:expected = 2147483648
30+
AssertEqual g:expected, Square(g:square)
31+
32+
Execute (grains on square 64):
33+
let g:square = 64
34+
let g:expected = 9223372036854775807
35+
AssertEqual g:expected, Square(g:square)
36+
37+
Execute (square 0 is invalid):
38+
let g:square = 0
39+
let g:expected = "square must be between 1 and 64"
40+
AssertThrows call Square(g:square)
41+
AssertEqual g:expected, g:vader_exception
42+
43+
Execute (negative square is invalid):
44+
let g:square = -1
45+
let g:expected = "square must be between 1 and 64"
46+
AssertThrows call Square(g:square)
47+
AssertEqual g:expected, g:vader_exception
48+
49+
Execute (square greater than 64 is invalid):
50+
let g:square = 65
51+
let g:expected = "square must be between 1 and 64"
52+
AssertThrows call Square(g:square)
53+
AssertEqual g:expected, g:vader_exception
54+
55+
Execute (returns the total number of grains on the board):
56+
let g:expected = 9223372036854775807
57+
AssertEqual g:expected, Total()

exercises/practice/grains/grains.vim

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"
2+
" Returns the number of grains on a chessboard square given the grains on each square
3+
" double from the previous square.
4+
" Throws an error if the square is below 1 or above 64.
5+
"
6+
" Examples:
7+
"
8+
" :echo Square(16)
9+
" 32768
10+
"
11+
" :echo Square(-1)
12+
" E605: Exception not caught: square must be between 1 and 64
13+
"
14+
function! Square(number) abort
15+
" your code goes here
16+
endfunction
17+
18+
"
19+
" Returns the total number of grains for a filled chessboard
20+
"
21+
function! Total(number) abort
22+
" your code goes here
23+
endfunction

0 commit comments

Comments
 (0)