Inside this module, import random and blackout_utils, and define the following global variable, which you should use whenever appropriate: OPERATIONS = ['^', 'x', '/', '+', '-'] For full marks, all the following functions must be part of this module: 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]

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

Inside this module, import random and blackout_utils, and define the following global variable, which you should use whenever appropriate:

OPERATIONS = ['^', 'x', '/', '+', '-']
For full marks, all the following functions must be part of this module:

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]

Expert Solution
steps

Step by step

Solved in 2 steps

Blurred answer
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY