On python please! def findNextOpr(s): #s must be a nonempty string. if len(s)<=0
ID: 3852190 • Letter: O
Question
On python please!
def findNextOpr(s):
#s must be a nonempty string.
if len(s)<=0 or not isinstance(s,str):
print("type mimatch error: findNextOpr")
return "type mimatch error: findNextOpr"
#In this exercise +, -, *, / are all the 4 operators
#The function returns -1 if there is no operator in s,
#otherwise returns the position of the leftmost operator
#--- code the rest of the function ----#
#--- function code ends -----#
def isNumber(s):
#s must be a non-empty string
#returns True if it's convertible to float, else False
if len(s)==0 or not isinstance(s, str):
print("type mismatch error: isNumber")
return "type mismatch error: isNumber"
#--- function code starts ---#
#--- function code ends ---#
def getNextNumber(expr, pos):
#expr is a given arithmetic formula in string
#pos = start position in expr
#1st returned value = the next number (None if N/A)
#2nd returned value = the next operator (None if N/A)
#3rd retruned value = the next operator position (None if N/A)
if len(expr)==0 or not isinstance(expr, str) or pos<0 or pos>=len(expr) or not isinstance(pos, int):
print("type mismatch error: getNextNumber")
return None, None, "type mismatch error: getNextNumber"
#--- function code starts ---#
#--- function code ends ---#
def exeOpr(num1, opr, num2):
#This is a simple utility function skipping type check
if opr=="+":
return num1+num2
elif opr=="-":
return num1-num2
elif opr=="*":
return num1*num2
elif opr=="/":
return num1/num2
else:
return None
def calc(expr):
#expr: nonempty string that is an arithmetic expression
#the fuction returns the calculated result
if not isinstance(expr,str) or len(expr)<=0:
print("argument error: line A in eval_expr") #Line A
return "argument error: line A in eval_expr"
#Hold two modes: "addition" and "multiplication"
#Initializtion: get the first number
newNumber, newOpr, oprPos = getNextNumber(expr, 0)
if newNumber is None:
print("input formula error: line B in eval_expr") #Line B
return "input formula error: line B in eval_expr"
elif newOpr is None:
return newNumber
elif newOpr=="+" or newOpr=="-":
mode="add"
addResult=newNumber #value so far in the addition mode
mulResult=None #value so far in the mulplication mode
elif newOpr=="*" or newOpr=="/":
mode="mul"
addResult=0
mulResult=newNumber
pos=oprPos+1 #the new current position
opr=newOpr #the new current operator
#start the calculation. Use the above functions effectively.
while True:
#--- code while loop ---#
#--- end of function ---#
Explanation / Answer
def findNextOpr(s):
#s must be a nonempty string.
if len(s)<=0 or not isinstance(s,str):
print("type mimatch error: findNextOpr")
return "type mimatch error: findNextOpr"
#In this exercise +, -, *, / are all the 4 operators
#The function returns -1 if there is no operator in s,
#otherwise returns the position of the leftmost operator
#--- code the rest of the function ----#
found = 0
position = 0
oprs = ['+','-','*','/']
for i in range(len(s)):
c = s[i]
if c in oprs:
found = 1
position = i
if (found == 1):
return position
else:
return -1
#--- function code ends -----#
def isNumber(s):
#s must be a non-empty string
#returns True if it's convertible to float, else False
if len(s)==0 or not isinstance(s, str):
print("type mismatch error: isNumber")
return "type mismatch error: isNumber"
#--- function code starts ---#
try:
x = float(s)
except ValueError:
return False
else:
return True
#--- function code ends ---#
def getNextNumber(expr, pos):
#expr is a given arithmetic formula in string
#pos = start position in expr
#1st returned value = the next number (None if N/A)
#2nd returned value = the next operator (None if N/A)
#3rd retruned value = the next operator position (None if N/A)
if len(expr)==0 or not isinstance(expr, str) or pos<0 or pos>=len(expr) or not isinstance(pos, int):
print("type mismatch error: getNextNumber")
return None, None, "type mismatch error: getNextNumber"
#--- function code starts ---#
oprs = ['+','-','*','/']
position = 0
for i in range(pos, len(expr)-1,1):
if(expr[i+1].isdigit):
nextNum = expr[i+1]
if expr[i] in oprs:
nextOpr = expr[i]
position = i
return (nextNum, nextOpr, position)
#--- function code ends ---#
def exeOpr(num1, opr, num2):
#This is a simple utility function skipping type check
if opr=="+":
return num1+num2
elif opr=="-":
return num1-num2
elif opr=="*":
return num1*num2
elif opr=="/":
return num1/num2
else:
return None
def calc(expr):
#expr: nonempty string that is an arithmetic expression
#the fuction returns the calculated result
if not isinstance(expr,str) or len(expr)<=0:
print("argument error: line A in eval_expr") #Line A
return "argument error: line A in eval_expr"
#Hold two modes: "addition" and "multiplication"
#Initializtion: get the first number
newNumber, newOpr, oprPos = getNextNumber(expr, 0)
if newNumber is None:
print("input formula error: line B in eval_expr") #Line B
return "input formula error: line B in eval_expr"
elif newOpr is None:
return newNumber
elif newOpr=="+" or newOpr=="-":
mode="add"
addResult=newNumber #value so far in the addition mode
mulResult=None #value so far in the mulplication mode
elif newOpr=="*" or newOpr=="/":
mode="mul"
addResult=0
mulResult=newNumber
pos=oprPos+1 #the new current position
opr=newOpr #the new current operator
#start the calculation. Use the above functions effectively.
while True:
#--- code while loop ---#
#--- end of function ---#
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.