Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Stringly typed solution in Clear category for Funny Addition by veky
class Addend(str):
"""Where a number is stored as a string."""
@classmethod
def full_adder(cls, result, target, carry=False):
"""The sum of the digits result and target, with carry (0 or 1)."""
count, carry = '-' if carry else '0', False
succ = cls.maketrans('-0123456789',
'01234567890')
while count != target:
result, count = result.translate(succ), count.translate(succ)
if result == '0': carry = True
return result, carry
def __add__(*args):
n2, n1 = sorted(args, key=len)
result, carry = [], False
for digits in zip(reversed(n1), reversed(n2.zfill(len(n1)))):
units, carry = Addend.full_adder(*digits, carry)
result.append(units)
if carry:
result.append('1')
return Addend(''.join(reversed(result)))
def checkio(data):
"""The sum of two integer elements"""
a, b = data
return int(Addend(a) + Addend(b))
Dec. 29, 2016
Comments: