-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
grains exercise: test against integer values as strings. (#326)
* To work around Vimscripts 2^63 limit for integers, the tests has been created to expect the integral values as strings.
- Loading branch information
Showing
6 changed files
with
88 additions
and
21 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
## 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. | ||
In other words, Vimscript cannot express any number `2^63` or greater as an integer. | ||
|
||
Some of the tests for this exercise require 64 bit integers which is beyond the integer size limitation ov Vimscript. | ||
Because of this limitation, the results of the calculations are tested against a string which expresses the integer value, rather than expressing the answer as Integer. | ||
Can you solve this by avoiding numbers that are larger than the language will allow directly? | ||
|
||
[number]: https://vimhelp.org/eval.txt.html#expr-number |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
{ | ||
"authors": [ | ||
"BNAndras" | ||
"BNAndras", | ||
"KOTP", | ||
"glennj" | ||
], | ||
"files": { | ||
"solution": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,59 @@ | ||
function! Square(number) abort | ||
if a:number < 1 || a:number > 64 | ||
throw 'square must be between 1 and 64' | ||
endif | ||
" Helper function to add two numbers represented as strings | ||
function! StringAdd(num1, num2) | ||
let carry = 0 | ||
let result = '' | ||
|
||
return float2nr(pow(2, (a:number-1))) | ||
" Pad the shorter number with leading zeros | ||
let len1 = strlen(a:num1) | ||
let len2 = strlen(a:num2) | ||
if len1 < len2 | ||
let a:num1 = repeat('0', len2 - len1) . a:num1 | ||
elseif len2 < len1 | ||
let a:num2 = repeat('0', len1 - len2) . a:num2 | ||
endif | ||
|
||
" Add digits from right to left | ||
for i in range(strlen(a:num1) - 1, 0, -1) | ||
let sum = str2nr(a:num1[i]) + str2nr(a:num2[i]) + carry | ||
let carry = sum >= 10 ? 1 : 0 | ||
let result = string(sum % 10) . result | ||
endfor | ||
|
||
" Add the last carry if it exists | ||
if carry > 0 | ||
let result = '1' . result | ||
endif | ||
|
||
return result | ||
endfunction | ||
|
||
function! Total() abort | ||
return float2nr(pow(2, 64) - 1) | ||
" Function to calculate grains on a specific square using string manipulation | ||
function! Square(n) | ||
if a:n < 1 || a:n > 64 | ||
throw 'square must be between 1 and 64' | ||
endif | ||
|
||
" Start with 1 grain on the first square | ||
let grains = '1' | ||
for i in range(2, a:n) | ||
" Double the grains by adding it to itself | ||
let grains = StringAdd(grains, grains) | ||
endfor | ||
|
||
return grains | ||
endfunction | ||
|
||
" Function to calculate the total grains on the chessboard using string manipulation | ||
function! Total() | ||
let total = '0' | ||
|
||
" Accumulate grains for each square from 1 to 64 | ||
let grains = '1' | ||
for i in range(1, 64) | ||
let total = StringAdd(total, grains) | ||
" Double grains for the next square | ||
let grains = StringAdd(grains, grains) | ||
endfor | ||
|
||
return total | ||
endfunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters