Question
Evaluate Mathematical Expression (using Stacks) Given a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate
Evaluate Mathematical Expression (using Stacks) Given a string s which represents an expression, evaluate this expression and return its value. The integer division should truncate toward zero. You may assume that the given expression is always valid. Note: You are NOT allowed to use any built-in function, or external library, which evaluates strings as mathematical expressions, such as eval(). Constraints: s consists of integers and operators '+', '-', '*', and '/' separated by some number of spaces. No other characters and assume the expression is valid operator precedence -, +, *, / (low to high) (note: this is slightly different from math; - and + are NOT in the same level; so do * and /) s represents a valid expression. All the integers in the expression are non-negative integers in the range [0, 2^31 - 1]. Point Allocation: 6 points for passing all test cases. ( you should always document your code, even if it's not required! ) Submission Instructions: A single .py file named evaluate_expression.py containing all of the functions listed above. Make sure the return types of all functions follow that of the Problem Statement. Returning the wrong type will make grading difficult As mentioned, please make sure to use the exact name of the file, or you will get a 0 on the assignment. You may use the starting code below as a starting template for your submission. Please make sure to keep to the naming conventions listed below, even if you do not use the template. Failure to follow naming conventions will fail the autograders and result in a 0 on the assignment. The autograder will also check the stack implementation in your code. class EvalExpression: def evaluate(self, s: str) -> int: ''' - Parameters: - s : expression to be evaluated - Returns: - int : result of the expression Iterate through the expression and calculate the expression using stacks. ''' # Your code here Example 1: Input: s = "3+2*2" Output: 7 Input: s = " 3/2 " Output: 1 Input: s = " 3+5 / 2 " Output: 5 Input: s = " 23 + 4 / 2 " Output: 25 Input: s = " 1000 - 400 / 2 " Output: 800
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started