Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Python Problem: Test: Problem 3: Expression simplification For this problem, you will write a function simplifye1 that, given an expression, replaces all subexpressions of the

Python Problem:

image text in transcribed

Test:

image text in transcribed

Problem 3: Expression simplification For this problem, you will write a function simplifye1 that, given an expression, replaces all subexpressions of the form (*,1, e) or (*,, 1) by e, and replaces all subexpressions of the form (+,0, e), (+, e, 0) (-, e, 0), by e. Note that you have to perform this in recursive fashion, performing the simplification from the bottom up in the expression. To be specific, for an expression (0,e1,e2), you have to first perform the simplification on ei and e2, obtaining e, and e, respectively. Once this is done, you consider the resulting expression (o, e, es), and you perform the simplification on that expression. [] def simplify@1(expr): "Takes an expression expr and returns a simplified version of it, where all subexpressions of the form ('*', 1, e) or ("*', 'e', 1), and all subexpressions of the form ('+', 0, e), ('+', 'e', o), or ('-', 'e', o), are replaced by e, leaving expr otherwise unchanged."" # YOUR CODE HERE raise Not ImplementedError() [ ] ### Feel free to use this cell to write your own tests. ### You will not be graded on what you write here. [ ] ### Tests for 'simplify01': base cases numbers and variables) assert_equal (simplify01 ('x'), 'x') assert_equal (simplify01 ('e'), 'e') assert_equal (simplify01(5), 5) assert_equal(simplify01(O), 0) [ ] ### Tests for simplify01": cases where the expression is unchanged assert_equal(simplify01(('+', 'x', 'x'), ('+', 'x', 'x')) assert_equal(simplify01(('+', 3, 4)), ('+', 3, 4)) assert_equal (simplify@1(('+', 1, 'e'), ('+', 1, 'e')) assert_equal(simplify01(('*', 0, 'e']), ('*', 0, 'e')) # For this problem, don't simplify multiplication by zero assert_equal (simplify01(('*', 'x', ('+', 3, 4))), ('*', 'x', ('+', 3, 4))) assert_equal (simplify01(("*', ('-', 3, 4), 2)), ('*', ('-', 3, 4), 2)) [ ] ### Tests for 'simplify01': single-operator expressions assert_equal (simplify01(('*', 1, 'y')), 'y') assert_equal(simplify01(('*', 6, 1)), 6) assert_equal(simplify01(('+', 0, 'x')), 'x') assert_equal(simplify01(('+', 3, 6)), 3) assert_equal (simplifyi(('-', 'x', 0)), 'x') [ ] ### Tests for "simplify01: more deeply nested expressions assert_equal(simplify01(('-', 8, ('-', 8, 0))), ('-', 8, 8)) assert_equal (simplify01(('+', ('-', 3, 4), ('+', 0, 0))), ('-', 3, 4)) assert_equal(simplify01(('*', ('-', 8, 6), ('-', 10, 0))), ('*', 8, 10)) assert_equal (simplify01(('*', 'x', ('+', 1, ))), 'x') assert_equal (simplify01(c'-', ('*', 1, 6), ('*', 1, 0))), o)

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Graph Database Modeling With Neo4j

Authors: Ajit Singh

2nd Edition

B0BDWT2XLR, 979-8351798783

More Books

Students also viewed these Databases questions

Question

Identify five strategies to prevent workplace bullying.

Answered: 1 week ago