Question: ### Fraction form To gain practice working with symbolic expression trees, we will implement a function that transforms an expression into _ fraction form. _

### Fraction form
To gain practice working with symbolic expression trees, we will implement a function that transforms an expression into _fraction form._ We say that an expression $e$ is in _fraction form_ if one of these two conditions is true:
* either $e$ does not contain the division operator $/$,
* or $e = e_1/ e_2$, for $e_1$ and $e_2$ not containing $/$.
Thus, intuitively, an expression $e$ in fraction form either does not contain division, or is in the form of a fraction, with a numerator and a denominator, neither of which contains the division operator.
In order to put an expression in fraction form, we start bottom up, obtaining fraction representations for the expression nodes proceeding from the leaves, and going up to the top, in fashion not dissimilar to what we did in the compute function. At a node $(\odot, e_1, e_2)$, given fraction representations for $e_1$ and $e_2$, we obtain a fraction representation for the node via:
$$
\frac{n_1}{d_1}\pm \frac{n_2}{d_2}\Rightarrow \frac{n_1 d_2\pm n_2 d_1}{d_1 d_2},\quad
\frac{n_1}{d_1}\cdot \frac{n_2}{d_2}\Rightarrow \frac{n_1 n_2}{d_1 d_2},\quad
\frac{n_1}{d_1}\Bigm/\frac{n_2}{d_2}\Rightarrow \frac{n_1 d_2}{d_1 n_2}.
$$
The implementation proceeds as follows. Given a node $(\odot, e_1, e_2)$, we first put $e_1$, $e2$ in fraction form, obtaining $e'_1, e'_2$. We then determine whether one of $e'_1$ or $e'_2$ is a fraction, that is, has the $/$ operator as the root operator. If this is the case, we combine the fractions $e'_1$ and $e'_2$ using the rules above. If none of them is a fraction, we simply leave the node unchanged. We leave the code for you to write.
#@title Putting expressions in fraction form.
def to_fraction(e):
"""Returns the expression e converted to fraction form."""
## YOUR SOLUTION HERE
Needs to pass the following tests:
e =('+',('/','a','b'),('/','c',2))
print(to_fraction(e))
e =('/','a',('/','b',('/','c','d')))
print(to_fraction(e))
# Tests 5 points: Simple tests for fraction form.
e =('/',('/','a',3),('/',4,'c'))
assert to_fraction(e)==('/',('*','a','c'),('*',3,4))
e =('*',('/','a',3),('/',4,'c'))
assert to_fraction(e)==('/',('*','a',4),('*',3,'c'))
e =('-',('/','a',3),('/',4,'c'))
assert to_fraction(e)==('/',('-',('*','a','c'),('*',4,3)),('*',3,'c'))
# Tests 5 points: More complicated tests for fraction form.
e =('/',('/','a',('/','b','v')),('*',('+','f','g'),'c'))
assert to_fraction(e)==('/',
('*',('*','a','v'),1),
('*',('*',1,'b'),('*',('+','f','g'),'c')))
e =('-',('+','a',('/','b','v')),('*',('+','f','g'),'c'))
assert to_fraction(e)==('/',
('-',
('*',('+',('*','a','v'),('*','b',1)),1),
('*',('*',('+','f','g'),'c'),('*',1,'v'))),
('*',('*',1,'v'),1))
# Finally, here are some randomized tests.
# This generates random expressions.
op_names ='+-*/'
var_names = 'abcdefghilmnopqrstuvz'
def random_expression(bias=0):
"""Returns a random expression."""
p =(1- math.exp(-bias /7))
if random.random()< p /3:
# Number.
return random.random()*10
elif random.random()< p:
# Symbol.
return "".join([random.choice(var_names) for _ in range(16)])
else:
op = random.choice(op_names)
return (op, random_expression(bias=bias+1), random_expression(bias=bias+1))
def is_in_fraction_form(e):
"""Returns True if e is in fraction form, and False otherwise."""
## YOUR SOLUTION HERE
Needs to pass the following test:
# Tests 10 points: These are the random tests.
class NotEqual(Exception):
pass
for _ in range(100):
e = random_expression()
ee = to_fraction(e)
assert is_in_fraction_form(ee),("not in form:", ee)
try:
assert value_equality(e, ee, num_samples=100, tolerance=0.1),("e:", e,"ee:", ee)
except:
raise NotEqual("e: {}, ee: {}".format(e, ee))
Please fill in everywhere it says '##YOUR SOLUTION HERE''

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!