Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing Fundamentals Design

Authors: Marion Donnie Dutton Don F. Seaman

14th Edition Globel Edition

ISBN: 1292107634, 978-1292107639

More Books

Students also viewed these Databases questions