-
Notifications
You must be signed in to change notification settings - Fork 182
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Partial functionality #1
base: master
Are you sure you want to change the base?
Conversation
I'm struggling with this lab. I tried your code, and got back roughly what some of my earlier code did, not produce good results. In the edit above, I changed some of the professor's code, and got partial functionality.
I came up with a solution. It uses recursion, which is what Professor Winston was hinting at in the handout/pdf. `# Section 3: Algebraic simplification This code implements a simple computer algebra system, which takes in anexpression made of nested sums and products, and simplifies it into asingle sum of products. The goal is described in more detail in theproblem set writeup.Much of this code is already implemented. We provide you with arepresentation for sums and products, and a top-level simplify() functionwhich applies the associative law in obvious cases. For example, itturns both (a + (b + c)) and ((a + b) + c) into the simpler expression(a + b + c).However, the code has a gap in it: it cannot simplify expressions that aremultiplied together. In interesting cases of this, you will need to applythe distributive law.Your goal is to fill in the do_multiply() function so that multiplicationcan be simplified as intended.Testing will be mathematical: If you return a flat list thatevaluates to the same value as the original expression, you willget full credit.We've already defined the data structures that you'll use to symbolicallyrepresent these expressions, as two classes called Sum and Product,defined below. These classes both descend from the abstract Expression class.The top level function that will be called is the .simplify() method of anExpression.>>> expr = Sum([1, Sum([2, 3])])>>> expr.simplify()Sum([1, 2, 3])Expression classes _____________________________________________________Expressions will be represented as "Sum()" and "Product()" objects.These objects can be treated just like lists (they inherit from the"list" class), but you can test for their type using the "isinstance()"function. For example:>>> isinstance(Sum([1,2,3]), Sum)True>>> isinstance(Product([1,2,3]), Product)True>>> isinstance(Sum([1,2,3]), Expression) # Sums and Products are both ExpressionsTruefrom depth import depth class Expression: class Sum(list, Expression):
class Product(list, Expression):
def simplify_if_possible(expr): You may find the following helper functions to be useful."multiply" is provided for you; but you will need to write "do_multiply"if you would like to use it.def multiply(expr1, expr2): def do_multiply(expr1, expr2):
if name == "main":
|
I'm struggling with this lab. I tried your code, and got back roughly what some of my earlier code did, not produce good results.
In the edit above, I changed some of the professor's code, and got partial functionality.