Solution to problem on slide 4 :
carry = 0
result = ''
a = list(a)
b = list(b)
while a or b or carry:
# basically if a or b still exists add that digit to carry, then write number and move to next digit
if a:
carry += int(a.pop())
if b:
carry += int(b.pop())
result += str(carry %2)
carry //= 2
# return flipped string
return result[::-1]
Solution to problem on slide 5 :
This solution is pretty good. It's O(n) time. Essentially all you do is cut both lists to the same length, then iterate both lists simultaneously until you reach an identical node.
Solution to problem on slide 6 : (HW)
Essentially start from the end of the list, and add 1 to the digit. If the digit is 10, then carry the 1 to the next digit, else you are done. If you reach the end of the list with a carry, add a 1 to the new digit.
i = 1
while(True):
if i==len(digits)+1:
digits = [1]+digits
break
digits[-i]+=1;
if digits[-i]==10:
digits[-i]=0
i+=1
else:
break
return digits