import algolab
import sys
#sys.path.append('../exams/2017-02-16-lab/solutions')
sys.path.append('past-exams/2017-02-16/solutions')
import nxpd
algolab.init()
There won't be any internet access. You will only be able to access:
One bonus point can be earned by writing stylish code. You got style if you:
avoid convoluted code like i.e.
if x > 5:
return True
else:
return False
when you could write just
return x > 5
%%HTML
<p class="algolab-warn">
!!!!!!!!! WARNING !!!!!!!!!
<br/>
<br/>
!!!!!!!!! **ONLY** IMPLEMENTATIONS OF THE PROVIDED FUNCTION SIGNATURES WILL BE EVALUATED !!!!!!!!! <br/>
</p>
For example, if you are given to implement:
def cool_fun(x):
raise Exception("TODO implement me")
and you ship this code:
def cool_fun_non_working_trial(x):
# do some absurdity
def cool_fun_a_perfectly_working_trial(x):
# a super fast, correct and stylish implementation
def cool_fun(x):
raise Exception("TODO implement me")
We will assess only the latter one cool_fun(x)
, and conclude it doesn't work at all :P !!!!!!!
Still, you are allowed to define any extra helper function you might need. If your cool_fun(x)
implementation calls some other function you defined like my_helper
here, it is ok:
def my_helper(y,z):
# do something useful
def cool_fun(x):
my_helper(x,5)
# this will get ignored:
def some_trial(x):
# do some absurdity
In /usr/local/esame you should find a file named algolab-17-01-26.zip
. Download it and extract it on your desktop. The content should be like this:
algolab-17-01-26
|- FIRSTNAME-LASTNAME-ID
|- exercise1-slow.py
|- exercise1-fast.py
|- exercise2.py
|- exercise3.py
2) Check this folder also shows under /var/exam
.
3) Rename FIRSTNAME-LASTNAME-ID
folder: put your name, lastname an id number, like john-doe-432432
From now on, you will be editing the files in that folder. At the end of the exam, that is what will be evaluated.
4) Edit the files following the instructions in this worksheet for each exercise.
%%HTML
<p class="algolab-warn">
WARNING: <i>DON'T</i> modify function signatures! Just provide the implementation.
</p>
<p class="algolab-warn">
WARNING: <i>DON'T</i> change the existing test methods, just add new ones !!! You can add as many as you want.
</p>
<p class="algolab-warn">
WARNING: <i>DON'T</i> create other files. If you still do it, they won't be evaluated.
</p>
<p class="algolab-important">
IMPORTANT: Pay close attention to the comments of the functions.
</p>
<p class="algolab-important">
IMPORTANT: if you need to print some debugging information, you <i>are allowed</i> to put extra <code>print</code>
statements in the function bodies.
</p>
<p class="algolab-warn">
WARNING: even if <code>print</code> statements are allowed, be careful with prints that might
break your function, i.e. avoid stuff like this: <code> print 1/0 </code>
</p>
3) Every exercise should take max 25 mins. If it takes longer, leave it and try another exercise.
%%HTML
<p class="algolab-warn">
WARNING: MAKE SURE ALL EXERCISE FILES AT LEAST COMPILE !!! <br/> 10 MINS BEFORE THE END
OF THE EXAM I WILL ASK YOU TO DO A FINAL CLEAN UP OF THE CODE
</p>
You are given a class BoolStack
that models a simple stack. This stack is similar to the CappedStack
you already saw in class, the only differences being:
ValueError
pop
or peek
an empty stack will raise an IndexError
from exercise1_slow_solution import *
To create a BoolStack
, just call it:
bs = BoolStack()
print bs
bs.push(True)
print bs
bs.push(False)
print bs
print bs.pop()
print bs
print bs.pop()
print bs
BoolStack
¶Now start editing the file exercise1_slow.py
. To check your environment is working fine, try to run the tests for BoolStackTest
, which contain tests for the already implemented methods pop
, push
, etc ...
Notice that exercise1_slow
is followed by a dot and test class name: .BoolStackTest
python -m unittest exercise1_slow.BoolStackTest
algolab.run(BoolStackTest)
true_count
, slow version¶Implement the true_count
method inside the class, just working on this method alone:
def true_count(self):
""" Return the number of elements which are True in O(n), where n is the size of stack. """
raise Exception("TODO IMPLEMENT ME !")
Testing
Once done, running this will run only the tests in TrueCountTest
class and hopefully they will pass.
Notice that exercise1_slow
is followed by a dot and test class name .TrueCountTest
:
python -m unittest exercise1_slow.TrueCountTest
algolab.run(TrueCountTest)
true_count
, fast version¶Now start editing the file exercise1_fast.py
: inside you will find the class FastBoolStack
. Your goal now is to implement a true_count
method that works in O(1)
. To make this possible, you are allowed to add any field you want in the constructor and you can also change any other method you deem necessary (like push
) .
def true_count(self):
""" Return the number of elements which are True
*** MUST EXECUTE IN O(1) ***
"""
raise Exception("TODO IMPLEMENT ME !")
Testing :
%%HTML
<p class="algolab-warn">
WARNING: Since you are going to modify the whole class, make sure tests pass BOTH for <code>FastBoolStackTest</code> AND <code>TrueCountTest</code> !
</p>
Tests for push
, pop
, etc:
python -m unittest exercise1_fast.FastBoolStackTest
Tests just for true_count
:
python -m unittest exercise1_fast.TrueCountTest
from exercise1_fast_solution import *
algolab.run(FastBoolStackTest)
algolab.run(TrueCountTest)
Start editing file exercise2.py
, which contains a simplified versioned of the UnorderedList
we saw in the labs.
from exercise2_solution import *
dup_first
¶Implement the method dup_first
:
def dup_first(self):
""" Modifies this list by adding a duplicate of first node right after it.
For example, the list 'a','b','c' should become 'a','a','b','c'.
An empty list remains unmodified.
** DOES NOT RETURN ANYTHING !!! **
"""
raise Exception("TODO IMPLEMENT ME !")
Testing: python -m unittest exercise2.DupFirstTest
algolab.run(DupFirstTest)
dup_all
¶Implement the method dup_all
:
def dup_all(self):
""" Modifies this list by adding a duplicate of each node right after it.
For example, the list 'a','b','c' should become 'a','a','b','b','c','c'.
An empty list remains unmodified.
** MUST PERFORM IN O(n) WHERE n is the length of the list. **
** DOES NOT RETURN ANYTHING !!! **
"""
raise Exception("TODO IMPLEMENT ME !")
Testing: python -m unittest exercise2.DupAllTest
algolab.run(DupAllTest)
Now you are going to build some DiGraph
, by defining functions external to class DiGraph
.
%%HTML
<p class="algolab-warn" target="_blank">
WARNING: To build the graphs, just use the methods you find inside <code>DiGraph</code> class, like <code>add_vertex</code>,
<code>add_edge</code>, etc.
</p>
Start editing file exercise3.py
from exercise3_solution import *
Implement the function pie
. Note the function is defined outside DiGraph
class.
"""
Returns a DiGraph with n+1 verteces, displaced like a polygon with a perimeter
of n verteces progressively numbered from 1 to n.
A central vertex numbered zero has outgoing edges to all other verteces.
For n = 0, return the empty graph.
For n = 1, return vertex zero connected to node 1, and node 1 has a self-loop.
"""
raise Exception("TODO IMPLEMENT ME !")
Testing: python -m unittest exercise3.PieTest
algolab.run(DiGraphTest)
algolab.run(PieTest)
Example usage :
For n=5, the function creates this graph:
print pie(5)
Degenerate cases:
print pie(0)
print pie(1)
nxpd.draw(algolab.to_nx(pie(1)), show='ipynb')
A Flux Capacitor is a plutonium-powered device that enables time travelling. During the 80s it was installed on a Delorean car and successfully used to ride humans back and forth across centuries:
In this exercise you will build a Flux Capacitor model as a Y-shaped DiGraph
, created according to a parameter depth
. Here you see examples at different depths:
Implement the function flux
. Note the function is defined outside DiGraph
class:
def flux(depth):
""" Returns a DiGraph with 1 + (d * 3) numbered verteces displaced like a Flux Capacitor:
- from a central node numbered 0, three branches depart
- all edges are directed outward
- on each branch there are 'depth' verteces.
For example, for depth=2 we get the following graph (suppose arrows point outward):
4 5
\ /
1 2
\ /
0
|
3
|
6
"""
raise Exception("TODO IMPLEMENT ME !")
Testing: python -m unittest exercise3.FluxTest
algolab.run(FluxTest)
Usage examples
print flux(0)
print flux(1)
print flux(2)
print flux(3)