Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
String_addition solution in Creative category for Funny Addition by andrea.manno1
class Addend:
"""Where a number is stored as a string"""
# The only abilities needed to perform an addition are counting and
# determining which digit comes after any given one
successor = dict(zip("0123456789x", "12345678900"))
def __init__(self, n: str):
self.n = n
def _digit_sum(self, digit_1, digit_2, previous_remainder = False):
""" Return the digit that is the sum of the given ones"""
digit_3, remainder = "x" if previous_remainder else "0", False
while digit_3 != digit_2:
digit_3 = self.successor[digit_3]
digit_1 = self.successor[digit_1]
if digit_1 == "0":
remainder = True
return (digit_1, remainder)
def __add__(self, other):
n1, n2 = sorted((list(self.n[::-1]), list(other.n[::-1])), key = len, reverse = True)
remainder, new_num = False, []
for i, digit_1 in enumerate(n1):
digit_2 = "0" if i >= len(n2) else n2[i]
digit, remainder = self._digit_sum(digit_1, digit_2, remainder)
new_num.append(digit)
if remainder:
new_num.append("1")
return Addend("".join(new_num[::-1]))
def checkio(data):
"""The sum of two integer elements"""
a, b = data
return int((Addend(str(a)) + Addend(str(b))).n)
if __name__ == '__main__':
assert checkio([5, 5]) == 10, 'First'
assert checkio([7, 1]) == 8, 'Second'
print('All ok')
Dec. 29, 2014
Comments: