I figured it out. A solution without using built in function such as sum()
. First you define a function to subtract without the - operator. Then you can use that to solve it. Here you go!:
def getSum(a, b):
negative_number = None
if a == 0:
return b
elif b == 0:
return a
if a < 0 and b < 0:
pass
elif a < 0:
negative_number = a
elif b < 0:
negative_number = b
if (a < 0 and b < 0) and abs(a) == abs(b):
return int("-%s" % (str(getSum(abs(a), abs(b)))))
elif abs(a) == abs(b) and (a < 0 or b < 0):
return 0
elif negative_number != None:
x = getSum(abs(a), abs(b))
if x > 2*abs(negative_number):
return subtract(getSum(abs(a), abs(b)), 2*abs(negative_number))
else:
return subtract(2*abs(negative_number), getSum(abs(a), abs(b)))
while b != 0:
carry = a&b
a = a^b
b= carry<<1
return a
def subtract(x, y): #Subtraction function without - operator.
while (y != 0):
borrow = (~x) & y
x = x ^ y
y = borrow << 1
return x
print (getSum(-15, 16)) #Substitute -15 and 16 for any values to find the sum
Now try whatever posibilities you want. it should work well for most numbers except for a few corner cases which I will attack and I will post the full solution as soon as I have one.