From 0333c3c52215f540cb996db626b8c7f4741d6efa Mon Sep 17 00:00:00 2001 From: Simon Forman Date: Tue, 4 Oct 2022 12:05:41 -0700 Subject: [PATCH] That was easy! Add BigInts of the same sign. --- bigjoyints/big.py | 39 ++++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/bigjoyints/big.py b/bigjoyints/big.py index a5bd77c..5f9ab29 100644 --- a/bigjoyints/big.py +++ b/bigjoyints/big.py @@ -1,3 +1,5 @@ +from itertools import zip_longest +from pprint import pprint as P import unittest @@ -48,7 +50,26 @@ class BigInt: return self.add_like_signs(other) def add_like_signs(self, other): - return BigInt(23) + ''' + Add a BigInt of the same sign as self. + ''' + assert self.sign == other.sign + out = [] + carry = 0 + Z = zip_longest( + self.digits, + other.digits, + fillvalue=zero, # Elegant, but not efficient? + ) + for a, b in Z: + carry, digit = a.add_with_carry(b, carry) + out.append(digit) + if carry: + out.append(one) + result = BigInt() + result.sign = self.sign + result.digits = out + return result class OberonInt: @@ -57,6 +78,22 @@ class OberonInt: 32-bit, two's complement. ''' + def add_with_carry(self, other, carry): + ''' + In terms of single base-10 skool arithmetic: + + a, b in {0..9} + carry in {0..1} + + 9 + 9 + 1 = 18 + 1 = 19 + aka = 1,(8+1) = 1, 9 + ''' + c, digit = self + other + if carry: + z, digit = digit + one + assert not z, repr(z) + return c, digit + def __init__(self, initial=0): assert is_i32(initial) self.value = initial