SCC461 – Programming for Data Scientists
Programming for Data Scientists
SCC461 – Programming for Data Scientists
Week 7
Assignment
Write your answers on the Moodle Quiz. Feedback is immediate, and re-attempts are allowed
(“check button”), with a 10% penalty.
The code must have enough comments. Please note that the system only recognises # as
comments, and not """.
1. OOP Exercises (2%)
Read Chapter 15 of “How to Think Like a Computer Scientist”. This exercise is based
on the exercises of that chapter.
Consider the following Point class:
c l a s s Point :
# Create a new Point , at coo rd ina t e s x , y
de f i n i t ( s e l f , x=0, y=0):
# Create a new point at x , y
s e l f . x = x
s e l f . y = y
de f d i s t a n c e f r o m o r i g i n ( s e l f ) :
# Compute my d i s t anc e from the o r i g i n
re turn ( ( s e l f . x ** 2) + ( s e l f . y ** 2) ) ** 0 .5
de f s t r ( s e l f ) : # Print the po int
re turn ”({0} , {1} )” . format ( s e l f . x , s e l f . y )
# Your code w i l l come here . Remember to indent i t to be i n s i d e the c l a s s
Add the following methods to this class:
slope from origin(self), which returns the slope of the line joining the origin to the
point. It returns False if the slope cannot be calculated.
get line to(self, p), which returns the straight line equation between this point object
(self), and another point p. The coefficients of the line y = ax+ b must be returned
as a tuple of values (a, b). It returns False if the line equation cannot be calculated.
In both methods, the results must be rounded to 5 decimal places. That is, a number
like 2.345679 should be represented as 2.34568. Additionally, the value zero must be
represented as 0. Any other representation (e.g., -0.0, 0.0, 0.00000) is not going to be
accepted.
In order to avoid inconsistencies, there will not be test cases where the result before
rounding is a number with 6 decimal places ending with 5 (e.g., 2.123455).
1
2. FibonacciStack (3%)
In a Fibonacci sequence, every number is the sum of the two previous ones (https:
//en.wikipedia.org/wiki/Fibonacci_number). In this assignment, we will consider
the Fibonacci sequence that starts with the numbers 0 and 1, as follows:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144...
In this exercise, you must create an Abstract Data Type FibonacciStack. A FibonacciS-
tack works just like a Stack, but the xth item removed will be multiplied by the xth
number in the Fibonacci sequence. Remember that you need to create a class in order to
create an Abstract Data Type.
For example, let’s assume that we push: 100, 5, 15, 30, 2. When we pop these items we
would print: 0, 30, 15, 10, 300. (Since 2 × 0 = 0, 30 × 1 = 30, 15 × 1 = 15, 5 × 2 = 10,
100× 3 = 300).
In detail, the class FibonacciStack must contain the following methods:
init (self): Constructor.
push(self, item): Pushes the item to the top of the FibonacciStack. Here you can
assume that item is an integer. This method return True if the push is successful,
and False otherwise. The FibonacciStack must hold at most 100 items. Therefore,
it must return False if there is no space to store an item.
pop(self): Returns the corresponding item from the top of the FibonacciStack. As
described above, the item must be multiplied by the xth element of the Fibonacci
sequence, if that is the xth time pop is called.
empty(self): Clears the FibonacciStack to its original state. Note that this method
will reset the number of pops to 0.
is empty(self): Returns True if the FibonacciStack is empty, and False otherwise.
Attention: Your solution must be computationally efficient. If the code is too slow for a
large number of pops, it will fail due to timeout.