Created
September 11, 2017 23:37
My solution to Reddit Daily Programmer 331 (Easy)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# https://www.reddit.com/r/dailyprogrammer/comments/6ze9z0/20170911_challenge_331_easy_the_adding_calculator/ | |
# Make a calculator that uses addition only to calculate Sum, Difference, | |
# Product, Division, and Exponentation | |
# Python 3 does not have a defined min value for Int and cannot overflow, | |
# therefore can't use a clearer method to get a negative one. | |
# The find function returns -1 if it does not locate the search string. | |
negOne="a".find("b") | |
# | |
#Functions | |
# | |
def Invert(op1): | |
j=0 | |
if op1 > 0: #pos to neg | |
for i in range(0,op1): | |
j=Add(j,negOne) | |
elif op1 < 0: #neg to pos | |
for i in range(op1,0): | |
j+=1 | |
return(j) | |
def Add(op1, op2): | |
return (op1+op2) | |
def Absolute(op1): | |
if op1>=0: #non-negative | |
return op1 | |
else: #negative, count up from op1 to 0 | |
j=0 | |
for i in range(op1,0): | |
j+=1 | |
return(j) | |
def Sub(op1, op2): | |
invFlag=False | |
if(op2 > op1): #swap order to take smaller value from larger, mark to invert | |
origOp1=op1 | |
op1=op2 | |
op2=origOp1 | |
invFlag=True | |
j=0 | |
for i in range (op2,op1): | |
j+=1 | |
if invFlag: | |
j=Invert(j) | |
return(j) | |
def Mult(op1, op2): | |
result = 0 | |
negative=False #assume positive | |
# check for zeros | |
if op1==0 or op2==0: | |
return 0 | |
#Check for sign of result, mixed signs mean negative result | |
if (op1>0 and op2<0) or (op1<0 and op2>0): | |
negative=True | |
op1=Absolute(op1) #take absolute value even if not needed - simplifies code | |
op2=Absolute(op2) | |
for i in range(0,op2): | |
result = Add(result,op1) | |
if negative==False: | |
return(result) | |
else: | |
return(Invert(result)) | |
def Div(op1, op2): # 9 / 3 = 3 | |
negative=False #assume positive | |
if(op2==0): #div by zero | |
return("Error: Divide by zero") | |
if (op1>0 and op2<0) or (op1<0 and op2>0): | |
negative=True | |
op1=Absolute(op1) #take absolute value even if not needed - simplifies code | |
op2=Absolute(op2) | |
j=0 | |
acc=0 | |
while(acc < op1): | |
acc=Add(acc,op2) | |
j+=1 | |
if(acc==op1): #Whole number | |
if negative==False: | |
return(j) | |
else: | |
return(Invert(j)) | |
else: #non-integer | |
return("Error: Non-Integer") | |
def Exp(op1, op2): | |
#edge cases | |
if(op2==0): | |
return(1) | |
if(op1==0): | |
return(0) | |
if(op2 < 0): #negative exponents not allowed (non-integer) | |
return("Error: Non-Integer") | |
acc=op1 | |
for i in range(1,op2): | |
acc=Mult(acc,op1) | |
#print("i={} and acc={}".format(i, acc)) | |
return(acc) | |
def ParseInput(inputStr): | |
splitVals=inputStr.split() | |
return(splitVals[1], int(splitVals[0]), int(splitVals[2])) | |
def CalculatorAddOnly(inputStr): | |
inputList=ParseInput(inputStr) | |
if(inputList[0]=="-"): | |
outputVal=Sub(inputList[1],inputList[2]) | |
elif(inputList[0]=="+"): | |
outputVal=Add(inputList[1],inputList[2]) | |
elif(inputList[0]=="*"): | |
outputVal=Mult(inputList[1],inputList[2]) | |
elif(inputList[0]=="/"): | |
outputVal=Div(inputList[1],inputList[2]) | |
elif(inputList[0]=="^"): | |
outputVal=Exp(inputList[1],inputList[2]) | |
else: | |
outputVal="Unknown operator" | |
print(inputStr + " = {}".format(outputVal)) | |
# | |
# Main | |
# | |
CalculatorAddOnly("12 + 25") | |
CalculatorAddOnly("-30 + 100") | |
CalculatorAddOnly("100 - 30") | |
CalculatorAddOnly("100 - -30") | |
CalculatorAddOnly("-25 - 29") | |
CalculatorAddOnly("-41 - -10") | |
CalculatorAddOnly("9 * 3") | |
CalculatorAddOnly("9 * -4") | |
CalculatorAddOnly("-4 * 8") | |
CalculatorAddOnly("-12 * -9") | |
CalculatorAddOnly("100 / 2") | |
CalculatorAddOnly("75 / -3") | |
CalculatorAddOnly("-75 / 3") | |
CalculatorAddOnly("7 / 3") | |
CalculatorAddOnly("0 / 0") | |
CalculatorAddOnly("5 ^ 3") | |
CalculatorAddOnly("-5 ^ 3") | |
CalculatorAddOnly("-8 ^ 3") | |
CalculatorAddOnly("-1 ^ 1") | |
CalculatorAddOnly("1 ^ 1") | |
CalculatorAddOnly("0 ^ 5") | |
CalculatorAddOnly("5 ^ 0") | |
CalculatorAddOnly("10 ^ -3") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment