1. Homepage
  2. Exam
  3. [2021] Imperial College London - Principles of Programming - Q4 Boolean Algebra

[2021] Imperial College London - Principles of Programming - Q4 Boolean Algebra

This question has been solved
Engage in a Conversation

Question 4 Boolean algebra

The objective of this question is to implement a very small computer symbolic algebra package for boolean algebra. For simplicity of implementation, we will not implement the __str__ special method, so the intention is that the following code will work as follows: CourseNana.COM


CourseNana.COM

In [1]: from boolean.boolean import Symbol
 
In [2]: x = Symbol("x")
 
In [3]: y = Symbol("y")
 
In [4]: x & y | ~x
Out[4]: Or(And(Symbol('x'), Symbol('y')), Not(Symbol('x')))


CourseNana.COM

4 (a) CourseNana.COM

(i) (1 mark) CourseNana.COM

In the boolean.boolean package create a class Expr whose constructor takes an arbitrary number of operands and stores them as an attribute named operands. CourseNana.COM

(ii) (2 marks) CourseNana.COM

Implement the Expr.__repr__ special method. It should return the class name followed by brackets containing a comma-separated list of the repr of each operand. CourseNana.COM

(iii) (1 mark)
CourseNana.COM

Create a subclass Symbol of Expr representing a (boolean) algebraic symbol. The constructor should take and store a single value. Symbol objects should have an empty list of operands. CourseNana.COM

(iv) (1 mark) CourseNana.COM

Implement the Symbol.__repr__ special method. It should return the class name followed by the value in brackets. CourseNana.COM

4 (b) CourseNana.COM

(i) (2 marks) CourseNana.COM

Implement Or, And, and Not subclasses of Expr. These require no additional functionality, so the class body can simply be pass. CourseNana.COM


CourseNana.COM

(ii) (2 marks) CourseNana.COM

Implement the __or__, __and__, and __invert__ special methods on Expr to return Or, And, and Not with the relevant operands. CourseNana.COM


CourseNana.COM

4 (c) (7 marks) CourseNana.COM

The boolean.boolean module contains a simple recursive postorder tree visitor. Write a single dispatch function which conforms to the interface required by the visiting function of that visitor. This function should use boolean algebra identities to propagate Not down the expression tree so that after the visitor is applied, Not only applies to Symbol objects. For example: CourseNana.COM


CourseNana.COM

In [1]: from boolean.boolean import Symbol, postvisitor, expand_negation
 
In [2]: x = Symbol("x")
 
In [3]: y = Symbol("y")
 
In [4]: expr = ~(x & ~y | ~x)
 
In [5]: postvisitor(expr, expand_negation)
Out[5]: And(Or(Not(Symbol('x')), Symbol('y')), Symbol('x'))


CourseNana.COM

The transformations that your single dispatch function will need to apply are as follows: CourseNana.COM


CourseNana.COM

Expr Output of visitor function CourseNana.COM

And Or applied to the result of the visitor applied to the operands. CourseNana.COM

Or And applied to the result of the visitor applied to the operands. CourseNana.COM

Symbol Not applied to the Symbol. CourseNana.COM

Not If the operand is a Symbol then that Symbol, otherwise the visitor applied to the operand. CourseNana.COM

4 (d) CourseNana.COM

(i) (2 marks) CourseNana.COM

Ensure that your code passes Flake8. CourseNana.COM


CourseNana.COM

(ii) (2 marks) CourseNana.COM

Ensure that your code otherwise conforms to good programming style as we have learned in this course. CourseNana.COM


CourseNana.COM

There is no need to write any docstrings in this exam. CourseNana.COM

Get the Solution to This Question

WeChat (微信) WeChat (微信)
Whatsapp WhatsApp
Imperial College London代写,Principles of Programming代写,Python代写,Imperial College London代编,Principles of Programming代编,Python代编,Imperial College London代考,Principles of Programming代考,Python代考,Imperial College Londonhelp,Principles of Programminghelp,Pythonhelp,Imperial College London作业代写,Principles of Programming作业代写,Python作业代写,Imperial College London编程代写,Principles of Programming编程代写,Python编程代写,Imperial College Londonprogramming help,Principles of Programmingprogramming help,Pythonprogramming help,Imperial College Londonassignment help,Principles of Programmingassignment help,Pythonassignment help,Imperial College Londonsolution,Principles of Programmingsolution,Pythonsolution,