Skip to content

Commit

Permalink
change the max square for grains
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Oct 11, 2024
1 parent fd6a691 commit 277a0ed
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 17 deletions.
3 changes: 1 addition & 2 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,7 @@
"uuid": "99408193-d0ce-4c68-8cab-e680f3ed56a4",
"practices": [],
"prerequisites": [],
"difficulty": 2,
"status": "wip"
"difficulty": 2
},
{
"slug": "hello-world",
Expand Down
11 changes: 11 additions & 0 deletions exercises/practice/grains/.docs/instructions.append.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## Maximum integer value

According to [the Vim docs][number]:

> Assuming 64 bit numbers are used (see v:numbersize) an unsigned number is truncated to 0x7fffffffffffffff or 9223372036854775807.
That means that Vim cannot express any number `2^63` or greater as an integer.

For the purposes of this exercise, we will declare that in Vimland, chess boards are 7x7 and have **49** squares.

[number]: https://vimhelp.org/eval.txt.html#expr-number
6 changes: 3 additions & 3 deletions exercises/practice/grains/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
function! Square(number) abort
if a:number < 1 || a:number > 64
throw 'square must be between 1 and 64'
if a:number < 1 || a:number > 49
throw 'square must be between 1 and 49'
endif

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

function! Total() abort
return float2nr(pow(2, 64) - 1)
return float2nr(pow(2, 49) - 1)
endfunction
19 changes: 9 additions & 10 deletions exercises/practice/grains/grains.vader
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,28 @@ Execute (grains on 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
Execute (grains on square 49):
let g:square = 49
let g:expected = 281474976710656
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"
let g:expected = "square must be between 1 and 49"
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"
let g:expected = "square must be between 1 and 49"
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"
Execute (square greater than 49 is invalid):
let g:square = 50
let g:expected = "square must be between 1 and 49"
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
let g:expected = 562949953421311
AssertEqual g:expected, Total()
4 changes: 2 additions & 2 deletions exercises/practice/grains/grains.vim
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"
" 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.
" Throws an error if the square is below 1 or above 49.
"
" Examples:
"
" :echo Square(16)
" 32768
"
" :echo Square(-1)
" E605: Exception not caught: square must be between 1 and 64
" E605: Exception not caught: square must be between 1 and 49
"
function! Square(number) abort
" your code goes here
Expand Down

0 comments on commit 277a0ed

Please sign in to comment.