# Adder method of a binary calculator

This is the adder method of my binary calculator. I did not cover for the case that someone passes a non binary number into the calculator yet.

I am pretty sure I have covered every other edge case for the calculator. I am trying to learn refactoring and how to look for simpler ways to do things. I know that I have a bunch of if's, but I don't know any other way I could have made the logic for this. Could someone mentor me through how they would refactor my code and why?

class BinaryCalculator:

num_a = self.num_to_list(number1)
num_b = self.num_to_list(number2)
maxx = max(len(num_a), len(num_b))
minn = min(len(num_a), len(num_b))

if minn == len(num_b):
smaller = num_b
num_b =  * (maxx - minn)
for i in smaller:
num_b.append(i)
else:
smaller = num_a
num_a =  * (maxx - minn)
for i in smaller:
num_a.append(i)

print " ",''.join(map(str, num_a))
print "+",''.join(map(str, num_b))
print '----------'
carry_bit = 0
answer =  * (maxx + 1)

for bit in xrange(maxx - 1, -1, -1):
sum_bit = 0
if num_a[bit] == num_b[bit]:
if num_a[bit] == 1:
if carry_bit == 0:
carry_bit += 1
else:
sum_bit = 1
else:
if carry_bit == 1:
sum_bit = 1
carry_bit -= 1

else:
if carry_bit == 1:
sum_bit == 0
else:
sum_bit = 1

if carry_bit == 1:

def num_to_list(self, num):
num = str(num)
list_to_return = []
for bit in range(len(num)):
list_to_return.append(int(num[bit]))
return list_to_return

c = BinaryCalculator()



There are some issues with your code concerning the code design.

Why do you use a class for your BinaryCalculator? You don't have multiple instances of the class, you do not even give the class a meaningful constructor. This task should not be solved with OOP, but just with regular functions.

You can easily drop the class and define the two functions as top level functions, e.g:

def num_to_list(num):
..... your code goes here ...

if __name__ == '__main__':
print(res)


Second, you can reduce your code significantly with some utility functions, e.g for your num_to_list function:

def num_to_list(x):
return [int(n) for n in reversed(str(x))]


Third, for your bit_adder the function zip_longest from the package itertools comes in handy. This just zips two lists like a regular zip, but fills up the shorter list with default values.

That's why I reversed the bit order in the num_to_list function earlier.

With this function you can reduce your bit_adder to a more compact version:

# zip the two revered bit lists with a default fillvalue of 0
z = itertools.zip_longest(a, b, fillvalue=0)
# add each bit, this results of a list that could look like [1, 0, 2, 1]
added = [n + k for n, k in z]

# we still have to get rid of the two's, so we iterate through the list
# and take care of the overflows, similarly to your approach
carry = 0
if v != 2 and carry == 0:
continue
elif v == 0 and carry == 1: