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

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
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
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education