assignment2_Advanced Python_UHasselt
assignment2_Advanced Python_UHasselt
Assignment 2: OOP
Questions
Implement the following programs in python:
1. Implement Stack class that is used to create and instantiate stack objects. Stack is a data
structure that contains data but restricts accessing the saved data in a LIFO (last in first
out) manner. In other words, the last element added to the stack is the first one to be
removed. You are required to add at least the following instance methods to your class:
If you tried to pop an empty stack, then you should return a KeyError exception with the
custom message "You can not pop an empty stack.".
To know more about stacks and a possible implementation of it, check this link [1].
1
be (le)op(re) where le is the expression of the object, op is operator, and re is
expression.
Example
Let e be an InfixExpression whose expression has the value "A+B*C". Then
– e.update_expression("-", "D") updates the expression of the infix expression
e to be "(A+B*C)-(D)"; and
– e.update_expression("^", "D/(E-F)") updates the expression of the infix ex-
pression e to be "(A+B*C)^(D/(E-F))".
convert_to_postfix(self) that uses the class Stack to return a string which is the
expression in postfix notation. You have to consider only the following five arithmetic
operators: ^, * , / , + , and - .
Example
Let e1, e2, and e3 be three InfixExpression objects whose expressions have the
values "A+B*C", "A*B+C", and "A*(B+C)"; respectively. Then
– e1.convert_to_postfix()= "ABC*+";
– e2.convert_to_postfix()= "AB*C+"; and
– e3.convert_to_postfix()= "ABC+*".
Helper Functions You can define as many additional functions as you want. Help functions
should start with an underscore. But all solutions should not exceed 500 lines. In fact, 500 lines
is extremely generous bound. You need much less than that.
Grading The assignments are graded by both hand and unit tests. That means the results
of the functions should be precisely as expected by the given unit tests. Check the test file
test assignment2.py on Blackboard. Do not edit that file except for the imports if needed.
In grading we do not only look at functionality, but we also take code quality (including comments
in the code) into account.
References
[1] Peter Wentworth, Jeffrey Elkner, Allen B. Downey, and Chris Meyers. How to Think Like
a Computer Scientist. http://openbookproject.net/thinkcs/python/english3e/index.
html, 2012. [Online; accessed 14-July-2019].