Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

change the max square for grains #322

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 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
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
3 changes: 3 additions & 0 deletions exercises/practice/grains/.meta/tests.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ 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"
include = false

[1d47d832-3e85-4974-9466-5bd35af484e3]
description = "returns the number of grains on the square -> square 0 is invalid"
Expand All @@ -38,6 +39,8 @@ description = "returns the number of grains on the square -> negative square is

[a95e4374-f32c-45a7-a10d-ffec475c012f]
description = "returns the number of grains on the square -> square greater than 64 is invalid"
include = false

[6eb07385-3659-4b45-a6be-9dc474222750]
description = "returns the total number of grains on the board"
include = false
20 changes: 10 additions & 10 deletions exercises/practice/grains/grains.vader
Original file line number Diff line number Diff line change
Expand Up @@ -29,29 +29,29 @@ 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
Execute (returns the total number of grains on the board for 49 squares):
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