• calculate(tokens): Takes one list of tokens as input corresponding to a mathematical equation. The function will evaluate both sides of the equality and return a list of three tokens: the result of the left-hand side (integer), the equals sign (string), and the result of the right-hand side (integer). The equation may contain any of the operations in the OPERATIONS list, as well as opening and closing parentheses. Your function should evaluate both sides of the equation while respecting order of operations (that is, parentheses, followed by exponentiation, then multiplication and division, and then addition and subtraction).

C++ for Engineers and Scientists
4th Edition
ISBN:9781133187844
Author:Bronson, Gary J.
Publisher:Bronson, Gary J.
Chapter7: Arrays
Section7.5: Case Studies
Problem 3E
icon
Related questions
Question

In python please

• calculate(tokens): Takes one list of tokens as input corresponding to a mathematical equation.
The function will evaluate both sides of the equality and return a list of three tokens: the result of
the left-hand side (integer), the equals sign (string), and the result of the right-hand side (integer).
The equation may contain any of the operations in the OPERATIONS list, as well as opening and
closing parentheses. Your function should evaluate both sides of the equation while respecting order
of operations (that is, parentheses, followed by exponentiation, then multiplication and division,
and then addition and subtraction).
Equations may have multiple sets of parentheses and nested parentheses. The contents of the
inner-most nested parentheses should always be evaluated first. To determine where the inner-most
parentheses are in the list of tokens, it can be helpful to find the last opening parenthesis ( in the
list, and then find the first closing parenthesis ) that occurs after that point. Then you would
evaluate the contents of those parentheses to a single integer, and replace all the tokens between
those parentheses, and the two parentheses themselves, by that single integer.
If the list of tokens does not correspond to a valid equation (e.g., it has two operations or numbers
in a row, or an operation at the start or end of the list), then return the original tokens list without
performing any operations.
>> calculate([4, 'x', 2, '=', 9, 'x', 2])
[8, '=', 18]
>> calculate([5, 'x', '(', 4, '+', 2, )', '=', 49])
[30, '=', 49]
>>> calculate([3, 'x', 'x', 7])
[3, 'x', 'x', 7]
Transcribed Image Text:• calculate(tokens): Takes one list of tokens as input corresponding to a mathematical equation. The function will evaluate both sides of the equality and return a list of three tokens: the result of the left-hand side (integer), the equals sign (string), and the result of the right-hand side (integer). The equation may contain any of the operations in the OPERATIONS list, as well as opening and closing parentheses. Your function should evaluate both sides of the equation while respecting order of operations (that is, parentheses, followed by exponentiation, then multiplication and division, and then addition and subtraction). Equations may have multiple sets of parentheses and nested parentheses. The contents of the inner-most nested parentheses should always be evaluated first. To determine where the inner-most parentheses are in the list of tokens, it can be helpful to find the last opening parenthesis ( in the list, and then find the first closing parenthesis ) that occurs after that point. Then you would evaluate the contents of those parentheses to a single integer, and replace all the tokens between those parentheses, and the two parentheses themselves, by that single integer. If the list of tokens does not correspond to a valid equation (e.g., it has two operations or numbers in a row, or an operation at the start or end of the list), then return the original tokens list without performing any operations. >> calculate([4, 'x', 2, '=', 9, 'x', 2]) [8, '=', 18] >> calculate([5, 'x', '(', 4, '+', 2, )', '=', 49]) [30, '=', 49] >>> calculate([3, 'x', 'x', 7]) [3, 'x', 'x', 7]
Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
C++ for Engineers and Scientists
C++ for Engineers and Scientists
Computer Science
ISBN:
9781133187844
Author:
Bronson, Gary J.
Publisher:
Course Technology Ptr
C++ Programming: From Problem Analysis to Program…
C++ Programming: From Problem Analysis to Program…
Computer Science
ISBN:
9781337102087
Author:
D. S. Malik
Publisher:
Cengage Learning