-
-
Notifications
You must be signed in to change notification settings - Fork 24
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
Closed
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
277a0ed
change the max square for grains
glennj 811b20d
exclude original tests that are now modified
glennj 3f92419
reimplementation to include all the tests: use string addition
glennj 4f75446
give up on method notation to appease vint
glennj df89939
working version from KTOP
glennj 8ad565d
wording of additional instructions
glennj File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,12 @@ | ||
## 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 this exercise, you will return the values as strings. | ||
That means you will have to implement a way to do addition on two _string_ operands. | ||
|
||
[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 large 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not crazy about this line. Addition is not the only way to do this, so we will not have to necessarily do this, but it states that we will have to do so. The word "implement", with its definitions, is ambiguous, and it may the solution does not use (at least only) two operands of any kind.
Also, the exercise will have us return the values as strings, but that does not mean what the line after says it means.
Perhaps part of my angst here is the "that means" part that is stated 4 lines above as well.
How can we phrase this so that the restrictions in Vimscript is stated, but the solution is left to the exerciser to be thought and explored.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe:
"Can you solve this by avoiding numbers that are larger than the language will allow directly?"
The tests will inform that the returns must be strings, and if the tests change later, then we do not have to change that aspect. The communication is (hopefully) clear.
"which is beyond the integer size limitation of Vimscript."
"Because of the language limitations regarding integers, the results of the calculations are tested against a string which expresses the integer value, rather than expressing the answer as Integer."
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, notice that I do avoid the use of "implement" as the word has confusing definitions. "The implement implements an implementation, within another implement." This statement is clear, but unless you are a native English speaker, it is may be hard to follow.