Skip to content

Commit

Permalink
give up on method notation to appease vint
Browse files Browse the repository at this point in the history
  • Loading branch information
glennj committed Nov 8, 2024
1 parent 3f92419 commit 4f75446
Showing 1 changed file with 4 additions and 4 deletions.
8 changes: 4 additions & 4 deletions exercises/practice/grains/.meta/example.vim
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
" add two strings as numbers from right to left, as if by hand
function! StringAdd(a, b) abort
" zero-left-pad so the numbers are the same length
let len = [a:a->strlen(), a:b->strlen()]->max()
let len = max([strlen(a:a), strlen(a:b)])
let a = printf('%0*s', len, a:a)
let b = printf('%0*s', len, a:b)
let result = ""
let carry = 0

for i in range(len - 1, 0, -1)
let c = carry + a->strpart(i, 1)->str2nr() + b->strpart(i, 1)->str2nr()
let c = carry + str2nr(strpart(a, i, 1)) + str2nr(strpart(b, i, 1))
let result = $"{c % 10}{result}"
let carry = c / 10
endfor
return $"{carry}{result}"->trim('0', 1)
return trim($"{carry}{result}", '0', 1)
endfunction

" populate the squares, doubling each previous, cache in the script scope
let s:grains = ['1']
for i in range(1, 63)
eval s:grains->add(StringAdd(s:grains[i - 1], s:grains[i - 1]))
eval add(s:grains, StringAdd(s:grains[i - 1], s:grains[i - 1]))
endfor

" return the grains on square `number`
Expand Down

0 comments on commit 4f75446

Please sign in to comment.