Question: Hello, I need help for my Computer Science HW. Based on these codes, I have create a function named calc to compute an arithmetic expression
Hello, I need help for my Computer Science HW.
Based on these codes, I have create a function named calc to compute an arithmetic expression given as a string parameter expr 4.25 * 5 - 2.01 / 3 * 7 + 2.
The arithmetic expression may include numeric values, four arithmetic operators, and extra spaces. 4.25 * 5 - 2.01 / 3 * 7 + 2
PLEASE HELP!!
def findNextOpr(s): #s must be a nonempty string. if len(s)=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 num1um2 else: return None def calc(expr): #expr: nonempty string that is an arithmetic expression #the fuction returns the calculated result if len(expr)


# Complete the five functions def findNextOpr (s): #5 must be a nonempty string if len(s)0 or not isinstance(s,str): print( "type mimatch error: findNextOpr") return "type mimatch error: findNext Opr #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) #5 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 ret runed value = the next operator position (None if N/A) if len(expr)==0 or not isinstance(expr, str) or pos-0 or pos>= 1 en (expr) or not isinstance (pos, int): print( "type mismatch error: get NextNumber") 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 opr"-" return num1-num2 return num1 num2 return num1/um2 return None e l i f opr==../.. : else
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
