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

Conversation

glennj
Copy link
Contributor

@glennj glennj commented Oct 11, 2024

http://forum.exercism.org/t/odd-arithmetic-in-grains/8662

Since the exercise was in WIP status, no worries about invalidating existing solutions.

@glennj glennj marked this pull request as draft October 11, 2024 21:27
@BNAndras
Copy link
Member

I think we'd need to mark the affected tests as not included in tests.toml. We're changing the descriptions, inputs, and expected values.

@glennj glennj marked this pull request as ready for review October 12, 2024 13:28
Copy link
Member

@kotp kotp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am approving, but not saying that we must bring this in.

Still considering the value of "work with the limitations of the language to do what seems like it can not be done" as something that we often do.

@glennj glennj marked this pull request as draft October 18, 2024 12:52
@glennj
Copy link
Contributor Author

glennj commented Oct 18, 2024

moving the PR to draft while I ponder the reimplementation.

@glennj
Copy link
Contributor Author

glennj commented Nov 8, 2024

vint is failing

exercises/practice/grains/.meta/example.vim:4:17: unexpected token: -> (see vim-jp/vim-vimlparser)

Line 4 col 17 of the new example is

  let len = [a:a->strlen(), a:b->strlen()]->max()
" ..............^

Is vint going to make me do

  let len = max([strlen(a:a), strlen(a:b)])

I'm having trouble installing it locally:

$ python3 -m venv ~/python3
$ source ~/python3/bin/activate.fish
$ python3 -m pip install vim-vint
Collecting vim-vint
  Downloading vim_vint-0.3.21-py2.py3-none-any.whl.metadata (910 bytes)
Collecting PyYAML>=3.11 (from vim-vint)
  Downloading PyYAML-6.0.2-cp313-cp313-macosx_10_13_x86_64.whl.metadata (2.1 kB)
Collecting ansicolor>=0.2.4 (from vim-vint)
  Downloading ansicolor-0.3.2-py2.py3-none-any.whl.metadata (625 bytes)
Collecting chardet>=2.3.0 (from vim-vint)
  Downloading chardet-5.2.0-py3-none-any.whl.metadata (3.4 kB)
Downloading vim_vint-0.3.21-py2.py3-none-any.whl (89 kB)
Downloading ansicolor-0.3.2-py2.py3-none-any.whl (9.8 kB)
Downloading chardet-5.2.0-py3-none-any.whl (199 kB)
Downloading PyYAML-6.0.2-cp313-cp313-macosx_10_13_x86_64.whl (181 kB)
Installing collected packages: ansicolor, PyYAML, chardet, vim-vint
Successfully installed PyYAML-6.0.2 ansicolor-0.3.2 chardet-5.2.0 vim-vint-0.3.21
$ type -a vint
vint is /Users/glennj/python3/bin/vint
$ vint exercises/practice/grains/
Traceback (most recent call last):
  File "/Users/glennj/python3/bin/vint", line 5, in <module>
    from vint import main
  File "/Users/glennj/python3/lib/python3.13/site-packages/vint/__init__.py", line 1, in <module>
    from vint.bootstrap import (
    ...<3 lines>...
    )
  File "/Users/glennj/python3/lib/python3.13/site-packages/vint/bootstrap.py", line 5, in <module>
    from vint.linting.cli import CLI
  File "/Users/glennj/python3/lib/python3.13/site-packages/vint/linting/cli.py", line 4, in <module>
    import pkg_resources
ModuleNotFoundError: No module named 'pkg_resources'
$ python3 -m pip install pkg_resources
ERROR: Could not find a version that satisfies the requirement pkg_resources (from versions: none)
ERROR: No matching distribution found for pkg_resources

@glennj
Copy link
Contributor Author

glennj commented Nov 8, 2024

OK, going to stop here. I'm shocked that "vimlparser" can't recognize method notation.

@BNAndras
Copy link
Member

BNAndras commented Nov 8, 2024

I’ll leave it up to Victor, but I’m not sure vint is useful here. Tests are automatically generated. The example solution just needs to pass the CI so I don’t bother using a linter on the other tracks I maintain.

@kotp
Copy link
Member

kotp commented Nov 8, 2024

OK, going to stop here. I'm shocked that "vimlparser" can't recognize method notation.

" Helper function to add two large numbers represented as strings
function! StringAdd(num1, num2)
    let carry = 0
    let result = ''

    " 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 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 passes vint, and I believe should also satisfy past 64 locations, though of course we want to (probably) fail past 64 from the story parameters given.

Different approach, but let me know what you think.

@glennj glennj marked this pull request as ready for review November 8, 2024 13:33
@glennj glennj requested a review from kotp November 8, 2024 13:34
@glennj
Copy link
Contributor Author

glennj commented Nov 8, 2024

Thanks @kotp that was driving me nuts

Copy link
Member

@kotp kotp left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still approved, but I did not bring it in because of the wording in appends that might go too far in directing specific approach, or details of an approach we know will work.

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.
Copy link
Member

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.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 and thus cannot be solved using only integer arithmetic.
The tests have been written to expect the return values as strings.
How can you implement arithmetic operations that return strings?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How can you implement arithmetic operations that return strings?

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.

and thus cannot be solved using only integer arithmetic.

"which is beyond the integer size limitation of Vimscript."

The tests have been written to expect the return values as strings.

"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."

Copy link
Member

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.

@glennj glennj closed this Nov 8, 2024
@glennj glennj deleted the grains-int-limit branch November 8, 2024 20:24
@glennj
Copy link
Contributor Author

glennj commented Nov 8, 2024

Ah sh!t, I accidentally deleted the branch. I'll create a new PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants