-1 byte by porting Jenny_mathy's Mathematic answer.
-2 more bytes by better golfing :p
;SN$DFS:9
See the test suite.
How?
;SN$DFS:9 - Main link: list of numbers, [a,b] e.g. [348,53]
$ - last two links as a monad
S - sum 401
N - negate -401
; - concatenate [348,53,-401]
D - convert to decimal lists [[3,4,8],[5,3],[-4,0,-1]]
F - flatten [3,4,8,5,3,-4,0,-1]
S - sum 18
:9 - integer divide by nine 2
My 12 byte solution...
:⁵+
DUSç\>9S
A monadic link taking a pair of integers and returning the number of carries as an integer.
There is probably a shorter way though! There was!
Try it online! or see the test suite.
How
:⁵+ · Link 1: perform a carry: right-column's-digit-sum, a; left-colum's-digit-sum; b
⁵ · literal 10
: · a integer-divided by 10 - the carry amount
+ · add to b
DUSç\>9S · Main link: list of summands e.g. [348,52]
D · convert to decimal lists [[3,4,8],[5,2]]
U · upend (reverse each) [[8,4,3],[2,5]]
S · sum (add the digits up) [10,9,3]
\ · cumulative reduce with:
ç · last link (1) as a dyad [10,10,4]
9 · literal 9
> · greater than? [ 1, 1,0]
S · sum 2
Related. – Leaky Nun – 2017-06-11T10:31:35.367
14Suggested test case:
190192, 90909
(has a break in the carries). – Jonathan Allan – 2017-06-11T11:14:31.6775
From @Jenny_mathy 's answer: the number of carries is equal to the difference between (1) the sum of the digit sum of the two inputs and (2) the digit sum of the sum of the two inputs, divided by nine. This is because when there is a carry, you subtract 10 from and add 1 to the digit sum. For instance,
– JungHwan Min – 2017-06-11T14:14:50.6239+9
gives you18
, but the digit sum is9+9-10+1
because there is a carry.Related anarchy endless challenge – xnor – 2017-06-11T17:00:52.973
Can we assume the numbers fit in our language's int type? In particular for Python 2, should we deal with
repr
appending anL
for numbers above2**63-1
? – xnor – 2017-06-11T17:04:24.087@xnor yes, you can. – Leaky Nun – 2017-06-11T17:18:29.393