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 e$ for $e$ and $e$ 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 e$ given fraction representations for $e$ and $e$ we obtain a fraction representation for the node via:
$$
fracndpm fracndRightarrow fracn dpm n dd dquad
fracndcdot fracndRightarrow fracn nd dquad
fracndBigmfracndRightarrow fracn dd n
$$
The implementation proceeds as follows. Given a node $odot e e$ we first put $e$ $e$ in fraction form, obtaining $e e$ We then determine whether one of $e$ or $e$ is a fraction, that is has the $$ operator as the root operator. If this is the case, we combine the fractions $e$ and $e$ 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 tofractione:
Returns the expression e converted to fraction form."""
## YOUR SOLUTION HERE
Needs to pass the following tests:
e abc
printtofractione
e abcd
printtofractione
# Tests points: Simple tests for fraction form.
e ac
assert tofractioneac
e ac
assert tofractioneac
e ac
assert tofractioneacc
# Tests points: More complicated tests for fraction form.
e abvfgc
assert tofractione
av
bfgc
e abvfgc
assert tofractione
avb
fgcv
v
# Finally, here are some randomized tests.
# This generates random expressions.
opnames
varnames 'abcdefghilmnopqrstuvz'
def randomexpressionbias:
Returns a random expression."""
p math.expbias
if random.random p :
# Number.
return random.random
elif random.random p:
# Symbol.
return joinrandomchoicevarnames for in range
else:
op random.choiceopnames
return op randomexpressionbiasbias randomexpressionbiasbias
def isinfractionforme:
Returns True if e is in fraction form, and False otherwise."""
## YOUR SOLUTION HERE
Needs to pass the following test:
# Tests points: These are the random tests.
class NotEqualException:
pass
for in range:
e randomexpression
ee tofractione
assert isinfractionformeenot in form:", ee
try:
assert valueequalitye ee numsamples tolerancee: eee: ee
except:
raise NotEquale: ee: formate 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
Question Has Been Solved by an Expert!
Get step-by-step solutions from verified subject matter experts
Step: 2 Unlock
Step: 3 Unlock
