CODE SHOULD BE IN PYTHON    it should pass this test case: e = ('*', ('+', 'x', 2), ('/', 'x', 'yay')) assert variables(e) == {'x', 'yay'}

C++ Programming: From Problem Analysis to Program Design
8th Edition
ISBN:9781337102087
Author:D. S. Malik
Publisher:D. S. Malik
Chapter13: Overloading And Templates
Section: Chapter Questions
Problem 15SA
icon
Related questions
Question
write a function variables such that, if e is an expression, variables(e) is the set of variables that appear in it.
def variables():
 
CODE SHOULD BE IN PYTHON 
 
it should pass this test case:
e = ('*', ('+', 'x', 2), ('/', 'x', 'yay'))
assert variables(e) == {'x', 'yay'}

 

[8] def simplify(e):
if isinstance(e, tuple):
op, l, r = e
# We simplify the children expressions.
11 = simplify( Add text cell
simplify(r)
rr =
# We compute the expression if we can.
if isnumber(ll) and isnumber(rr) :
return calc(op, ll, rr)
else:
return (op, l1, rr)
else:
# Leaf. No simplification is possible.
return e
Transcribed Image Text:[8] def simplify(e): if isinstance(e, tuple): op, l, r = e # We simplify the children expressions. 11 = simplify( Add text cell simplify(r) rr = # We compute the expression if we can. if isnumber(ll) and isnumber(rr) : return calc(op, ll, rr) else: return (op, l1, rr) else: # Leaf. No simplification is possible. return e
[2]
Os
class IllegalOperator(Exception):
pass
Let us define a helper function calc,which takes as argument an operator and two numbers, and computes the required operation. It will make
it easier to write the rest of the code.
def calc(op, left, right):
if op == "+":
return left + right
elif op ==
"_":
return left -
right
elif op ==
"*":
return left * right
elif op == "/":
return left / right
else:
raise IllegalOperator(op)
With this, we can write our compute method as follows.
[ 4] def compute(e):
if isinstance(e, tuple):
# We have an expression.
op, 1, r = e
# We compute the subexpressions.
11 = compute(1)
rr = compute(r)
# And on the basis of those, the whole expression.
return calc (op, 11, rr)
else:
# base expression; just return the number.
return e
Transcribed Image Text:[2] Os class IllegalOperator(Exception): pass Let us define a helper function calc,which takes as argument an operator and two numbers, and computes the required operation. It will make it easier to write the rest of the code. def calc(op, left, right): if op == "+": return left + right elif op == "_": return left - right elif op == "*": return left * right elif op == "/": return left / right else: raise IllegalOperator(op) With this, we can write our compute method as follows. [ 4] def compute(e): if isinstance(e, tuple): # We have an expression. op, 1, r = e # We compute the subexpressions. 11 = compute(1) rr = compute(r) # And on the basis of those, the whole expression. return calc (op, 11, rr) else: # base expression; just return the number. return e
Expert Solution
Step 1

def variables(e):
    e = ('*', ('+', 'x', 2), ('/', 'x', 'yay'));
    assert variables(e) == {'x', 'yay'};

trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 2 steps

Blurred answer
Knowledge Booster
Reference Types in Function
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
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