Question: May I get help on the homework? This is the previous version of the homework, and now we have to add something. Please write the
May I get help on the homework? This is the previous version of the homework, and now we have to add something. Please write the code in Pyhon3, and provide the screenshots and the code to copy if possible. Thank you so much!
Below is the assignment:




And this is the starter code that I made, but I only got 80% on the previous assignment. Can you please see and make everything works fine? It would be very plausible if you provide the screenshots of the code and the code to copy. Please write the code in Python3
----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
class Node: def __init__(self, value): self.value = value self.next = None def __str__(self): return "Node({})".format(self.value)
__repr__ = __str__
# ---- Copy your Stack (or Queue) code from LAB9 (or LAB10) here --------- class Stack: def __init__(self): self.top = None def __str__(self): temp=self.top out=[] while temp: out.append(str(temp.value)) temp=temp.next out=' '.join(out) return ('Top:{} Stack: {}'.format(self.top,out))
__repr__=__str__ def isEmpty(self): return self.top is None
def __len__(self): cnt = 0 temp = self.top while temp: cnt += 1 temp = temp.next return cnt def peek(self): if self.isEmpty(): return "Stack is empty" return self.top.value
def push(self,value): n = Node(value) n.next = self.top self.top = n
def pop(self): if self.isEmpty(): return "Stack is empty" value = self.top.value self.top = self.top.next return value # ---- Stack (or Queue) code ends here here ---------
def findNextOpr(txt): if len(txt)
elif txt.find("+") != -1 or txt.find("-") != -1 or txt.find("/") != -1 or txt.find("^") != -1: for i in range(len(txt)): indexNum = txt[i] if (indexNum == "+" or indexNum == "-" or indexNum == "*" or indexNum == "/" or indexNum == "^"): return i break else: return -1
def isNumber(txt): if not isinstance(txt, str): return "type error: isNumber"
if len(txt) == 0: return False txt.replace(" ","")
try: float(txt) return True except ValueError: return False
def getNextNumber(expr, pos): if len(expr) == 0 or not isinstance(expr, str) or not isinstance(pos, int) or pos > len(expr) or pos
next_oprPos = findNextOpr(expr[pos:]) if next_oprPos > 0: next_oprPos += pos nextOpr = expr[next_oprPos] if isNumber(expr[pos:next_oprPos]): nextNum = float(expr[pos:next_oprPos]) else: nextOpr = None if isNumber(expr[pos:]): nextNum = float(expr[pos:]) return nextNum, nextOpr, next_oprPos
def exeOpr(num1, opr, num2): #This funtion is just an utility function. It is skipping type check if opr=="+": return num1+num2 elif opr=="-": return num1-num2 elif opr=="*": return num1*num2 elif opr=="/": if num2==0: print("Zero division error") return "error" else: return num1um2 elif opr=="^": return num1 ** num2 else: print("error in exeOpr") return "error"
def _calculator(expr): pos = 0 position = 1 nextOpr = 0 result = 0 addlastOpr = "+" mullastOpr = None explastOpr = "^"
if not isinstance(expr,str) or len(expr)
expr = expr.strip()
if expr[0] != "-": newNumber, newOpr, oprPos = getNextNumber(expr, 0) else: newNumber, newOpr, oprPos = getNextNumber(expr, 1) newNumber *= -1 if newNumber is None: return "error"
elif newOpr is None: return newNumber
elif newOpr == "+" or newOpr == "-": mode = "add" addResult = newNumber mulResult = None expResult = 0 elif newOpr == "*" or newOpr == "/": mode = "mul" addResult = 0 mulResult = newNumber expResult = 0 finalOpr = "+"
elif newOpr == "^": mode = "exp" addResult = 0 mulResult = None expResult = newNumber finalOpr = "+" while True: newNumber, newOpr, oprPos = getNextNumber(expr, oprPos + 1) if newNumber is None: return "error" elif newOpr is None and mode == "add": return exeOpr(addResult, addlastOpr, newNumber)
elif newOpr is None and mode == "mul": mulResult = exeOpr(mulResult, mullastOpr, newNumber) return exeOpr(addResult, addlastOpr, mulResult)
elif newOpr is None and mode == "exp": expResult = exeOpr(expResult, explastOpr, newNumber)
if mulResult is None: return exeOpr(addResult, addlastOpr, expResult)
else: mulResult = exeOpr(mulResult, mullastOpr, expResult) return exeOpr(addResult, addlastOpr, mulResult)
elif (newOpr == "+" or newOpr == "-") and mode == "add": addResult = exeOpr(addResult, addlastOpr, newNumber) addlastOpr = newOpr mode = "add"
elif (newOpr == "*" or newOpr == "/") and mode == "add": mulResult = newNumber mullastOpr = newOpr mode = "mul"
elif (newOpr == "^") and mode == "add": expResult = newNumber explastOpr = newOpr mode = "exp"
elif (newOpr == "+" or newOpr == "-") and mode == "mul": mulResult = exeOpr(mulResult, mullastOpr, newNumber)
if addlastOpr is None: addResult = mulResult
else: addResult = exeOpr(addResult, addlastOpr, mulResult) addlastOpr = newOpr mode = "add"
elif (newOpr == "*" or newOpr == "/") and mode == "mul": mulResult = exeOpr(mulResult, mullastOpr , newNumber) mode = "mul" mullastOpr = newOpr
def calculator(expr): # Required: calculator must create and use a Stack (or Queue) for parenthesis matching # Call _calculator to compute the inside parentheses if not isinstance(expr,str) or len(expr)
#Scan the expression to find the most inner expression, note that if pos==-1 you can try to compute the expression as is pos = expr.find("(") while True: #--- function code starts here -----#
#--- function code ends here-----#
Thank you!
Goal Modify the function calculator(expr) in HW3.py so that it supports parentheses in expr. An example of a valid expression is Notes Copy vour code from calculatorlexp into the function calculator(expr. Your calculator(expr) in HW3.py must call_calculator(expr) Modify getNextNumber to allow negation in expressions like this: "4+2*-4+1 Add the stack code from Lab 9 or you queue code from Lab 10 into your HW4 script. Your calculator(expr) function must use the stack or the queue for parenthesis matching otherwise, no credit will be given Function requirements The function must return the computed value if expr is a correct formula, otherwise it must return an error message V When any function returns a numeric value, it must be float Do not use exec or eval function. You will not receive credit if your program uses any of the two functions anywhere Grading Notes The grading script will feed 5 randomly chosen test inputs, each for 20 points. One of them will be an input that should cause an error such as "(4 * 5/ (2 + (5 ^2)', whose expected returned value is an error message Examples: >>>calculator ("3 (10 - 2*3) " 12.0 >>>calculator("2 / (4)(3 2*4- 2*3)) + 3") >>>calculator ("2 (4+2* (5-3 2) +1) +4") >>>calculator("- (-2) 10 -3* (23*2)") 32.0 >>>calculator("- (-2) *10 -3* (23*2)") Goal Modify the function calculator(expr) in HW3.py so that it supports parentheses in expr. An example of a valid expression is Notes Copy vour code from calculatorlexp into the function calculator(expr. Your calculator(expr) in HW3.py must call_calculator(expr) Modify getNextNumber to allow negation in expressions like this: "4+2*-4+1 Add the stack code from Lab 9 or you queue code from Lab 10 into your HW4 script. Your calculator(expr) function must use the stack or the queue for parenthesis matching otherwise, no credit will be given Function requirements The function must return the computed value if expr is a correct formula, otherwise it must return an error message V When any function returns a numeric value, it must be float Do not use exec or eval function. You will not receive credit if your program uses any of the two functions anywhere Grading Notes The grading script will feed 5 randomly chosen test inputs, each for 20 points. One of them will be an input that should cause an error such as "(4 * 5/ (2 + (5 ^2)', whose expected returned value is an error message Examples: >>>calculator ("3 (10 - 2*3) " 12.0 >>>calculator("2 / (4)(3 2*4- 2*3)) + 3") >>>calculator ("2 (4+2* (5-3 2) +1) +4") >>>calculator("- (-2) 10 -3* (23*2)") 32.0 >>>calculator("- (-2) *10 -3* (23*2)")
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
