WARNING: THIS IS THE OLD ALGOLAB WEBSITE FOR ACADEMIC YEAR 2016-17.
NEW WEBSITE IS AT: sciprog.davidleoni.it
In [1]:
import algolab
algolab.init()
Out[1]:
Module 2: Algorithm and Data Structures Lab


David Leoni
teaching assistant
david.leoni [AT] unitn.it

University of Trento

11 September 2017
v0.23


This work is licensed under a Creative Commons Attribution 4.0 License creativecommons.org/licenses/by/4.0/

In [2]:
%%HTML

<!--
<h1>  <a href="exam-2017-09-04.html" target="_blank" style="color:red"> 04/09/2017 EXAM TEXT</a></h1>
-->

<h1> News </h1>

<h4> 11/09/2017 </h4>
- published solutions of <a href="past-exams.html#2017-09-04" target="_blank"> 04/09/17 Exam</a> <br/>

<h4> 06/08/2017 </h4>
- published solutions of <a href="past-exams.html#2017-06-08" target="_blank"> 06/08/17 Exam</a> <br/>


<h4> 21/02/2017 </h4>
- published solutions of <a href="past-exams.html#2017-02-16" target="_blank"> 16/02/17 Exam</a> <br/>

<h4> 9/02/2017 </h4>
- fixed bugs in <a href="trees.html" target="_blank">methods of GenericTree</a>, added <code>detach_sibling</code> tests <br/>

<h4> 26/01/2017 </h4>
- published solutions of <a href="past-exams.html#2016-01-26" target="_blank"> 26/01/17 exam</a> <br/>

<h4> 23/01/2017 </h4>
- Fixed many bugs in <a href="graphs.html" target="_blank">graph</a> reports of differences during testing  <br/>

<h4> 13/01/2017 </h4>
- published solutions of <a href="past-exams.html#2017-01-13-midterm" target="_blank"> 13/01/17 Midterm</a> <br/>

<h4> 10/01/2017 </h4>
- Added a lot of material <a href="graphs.html" target="_blank">on graphs </a> <br/>

<h4> 9/01/2017 </h4>
- fixed broken link in Chapters to <a href="data-structures-2.html" target="_blank">Data Structures 2.2 for stacks and linked lists </a><br/>

<h4> 8/01/2017 </h4>
- added looong intro to classes in <a href="data-structures-1.html" target="_blank" >Data Structures 2.1:  ComplexNumber</a><br/>
- split Chapter 2: Data Structures into 2 parts, <a href="data-structures-1.html" target="_blank">2.1 for ComplexNumber</a> and <a href="data-structures-2.html" target="_blank">2.2 for stacks and linked lists.</a> <br/>

<h4>22/12/2016 </h4>
- added graph exercises

News

11/09/2017

- published solutions of 04/09/17 Exam

06/08/2017

- published solutions of 06/08/17 Exam

21/02/2017

- published solutions of 16/02/17 Exam

9/02/2017

- fixed bugs in methods of GenericTree, added detach_sibling tests

26/01/2017

- published solutions of 26/01/17 exam

23/01/2017

- Fixed many bugs in graph reports of differences during testing

13/01/2017

- published solutions of 13/01/17 Midterm

10/01/2017

- Added a lot of material on graphs

9/01/2017

- fixed broken link in Chapters to Data Structures 2.2 for stacks and linked lists

8/01/2017

- added looong intro to classes in Data Structures 2.1: ComplexNumber
- split Chapter 2: Data Structures into 2 parts, 2.1 for ComplexNumber and 2.2 for stacks and linked lists.

22/12/2016

- added graph exercises

Exams

See exams schedule on Alberto Montresor's site

Exam modalities

Algolab exams are open book. You can bring a printed version of the material listed below.

Exam will take place in the lab with no internet access. You will only be able to access a folder with this documentation:

So if you need to look up some Python function, please start today learning how to search documentation using the search functionality on Python website.

Past exams

See page here.

Chapters

Worksheets are meant to be used online - pdf quality is not very good, if they result unreadable please tell me

0. Testing (pdf)

1. Lists (pdf)

2 Data Structures

2.1 ComplexNumber (pdf)

2.2 Stacks and linked lists (pdf)

3. Trees (pdf)

4. Graphs (pdf)

A. Past exams

Commandments

In [3]:
%%HTML

<p class="algolab-warn">
WARNING: If you don't follow the Commandments, bad things happen! 
</p>

<p class="algolab-important" >
1) You shall test!
</p>

To run tests, enter the following command in the terminal:

WARNING: If you don't follow the Commandments, bad things happen!

1) You shall test!

To run tests, enter the following command in the terminal:
python -m unittest my-file
In [4]:
%%HTML

<p class="algolab-warn">
WARNING: In the call above, DON'T append the extension <i>.py</i> to <i>my-file</i>
<br/>
WARNING: Still, on the hard-disk the file MUST be named with a <i>.py</i> at the end, like <i>my-file.py</i>
</p>

<p class="algolab-important" >
2. You shall also write on paper!
</p>

<p>
If staring at the monitor doesn't work, help yourself and draw a representation of the state sof the program. 
Tables, nodes, arrows, all can help figuring out a solution for the problem. 
</p>

<p class="algolab-important" >
3. You shall copy *exactly the same* function definitions as in the exercises!
</p>

For example don't write :

WARNING: In the call above, DON'T append the extension .py to my-file
WARNING: Still, on the hard-disk the file MUST be named with a .py at the end, like my-file.py

2. You shall also write on paper!

If staring at the monitor doesn't work, help yourself and draw a representation of the state sof the program. Tables, nodes, arrows, all can help figuring out a solution for the problem.

3. You shall copy *exactly the same* function definitions as in the exercises!

For example don't write :
def MY_selection_sort(A):
In [5]:
%%HTML

<p class="algolab-important" >
4. You shall never ever reassign function parameters:
</p>

4. You shall never ever reassign function parameters:

def myfun(i, s, L, D):

        # You shall not do any of such evil, no matter what the type of the parameter is:
        i = 666            # basic types (int, float, ...)
        s = "666"          # strings
        L = [666]          # containers 
        D = {"evil":666}   # dictionaries

        # For the sole case of composite parameters like lists or dictionaries, 
        # you can write stuff like this IF AND ONLY IF the function specification 
        # requires you to modify the parameter internal elements (i.e. sorting a list
        # or changing a dictionary field):

        L[4] = 2          # list
        D.my_field = 5    # dictionary

This also applies to class methods. Never ever write horrors such as:

class MyClass
    def my_method(self, x, y):
        self = {a:666}  # since self is a kind of dictionary, you might be tempted to do like this
                        # but to the outside world this will bring no effect.
                        # For example, let's say somebody from outside makes a call like this:
                        #    mc = MyClass()
                        #    mc.my_method()
                        # after the call mc will not point to {a:666}
        self = ['666']  # self is only supposed to be a sort of dictionary and passed from outside
        self = 6        # self is only supposed to be a sort of dictionary and passed from outside
In [6]:
%%HTML

<p class="algolab-important" >
5. You shall never ever assign values to method calls:
</p>

5. You shall never ever assign values to method calls:

WRONG WRONG STUFF

my_fun() = 666
my_fun() = '666'
my_fun() = [666]

CORRECT STUFF

With the assignment operator we want to store in the left side a value from the right side, so all of these are valid operations:

x = 5
y = my_fun()
z = []
z[0] = 7
d = {}
d["a"] = 6

Function calls such as my_fun() return instead results of calculations in a box that is created just for the purpose of the call and Python will just not allow us to reuse it as a variable. So whenever you see 'name()' at the left side, it can't be possibly follewed by one equality = sign (but it can be followed by two equality signs == if you are performing a comparison).

In [7]:
%%HTML

<p class="algolab-important" >
6. You shall use <i>return</i> command only if you see written <i>return</i> in the pseudocode!
</p>

6. You shall use return command only if you see written return in the pseudocode!

If there is no return in the pseudocode, the function is intended to return None. In this case you don't even need to write return None, as Python will do it implicitly for you.

Slides

Lab 1 Slides

3 Nov 2016

Lab Goals

  • Going from theory taught by Prof. Alberto Montresor to implementation
  • Pseudo code --> Python

How

  • Hands-on approach
  • Performance considerations
  • Focus on correct code
  • Few Python functions

Lab Midterm?

Probably not. Still, will provide exam example.

Lab 2 Slides

Date: Nov 11th, 2016

  • More practical than last time!
  • Finish selection_sort and gap implementation
  • midlab pause ;-)
  • implement a Python class

Lab 3 Slides

Nov 17th, 2016

  • Recursion
    • gap_rec, binary_search_rec
  • binary_search_iter
  • Will give you more tests
  • Write also on paper!
  • Copy exactly the same function definitions!
    • For example don't write def MY_selection_sort(A):
  • use return command only if you see written return in the pseudocode!
    • If there is no return in the pseudocode, the function is intended to return None. In this case you don't even need to write return None, as Python will do it implicitly for you.

Lab 4 Slides

Nov 18th, 2016

  • Divide et Impera
    • binary_search_iter
  • Implement ComplexNumber ( see Chapter 2.1 )

New Commandment:

You shall never ever reassign function parameters:

def myfun(L, i, s):

    # You shall not do any of this evil:
    L = [666]
    i = 666
    s = "666"

Previous commandments:

  • You shall also write on paper!
  • You shall copy exactly the same function definitions as in the exercises!
  • For example don't write def MY_selection_sort(A):
  • You shall use return command only if you see Written return in the pseudocode!
  • If there is no return in the pseudocode, the function is intended to return None. In this case you don't even need to write return None, as Python will do it implicitly for you.

Lab 5 slides

24 Nov 2016

  • Implement ComplexNumber ( see Chapter 2.1 )
  • Implement Stack

Lab 6 slides

1 Dic 2016

  • Implement CappedStack ( see Chapter 2.2 )
  • Implement UnorderedList

Lab 7 slides

  • UnorderedList

Lab 8 slides

  • will start writing tests (will be required during exams)
  • invariants in particular
  • UnorderedList ( see Chapter 2.2 ) until fast size() and append() included

Lab 9 slides

15 Dic 2016

Lab 10 slides

19 Dic 2016

Lab 11 slides

21 Dic 2016

Exam simulation !

For exam solution, see Appendix A in Chapters

Office hours

You can schedule a meeting by emailing me at david.leoni [AT] unitn.it , more or less I'm available every day until 19.00

Then you will find me in Povo 1 building at DISI, in room 226 DKM :

Resources

Editors

  • Spyder: Seems like a fine and simple editor
  • Jupyter Notebook: Nice environment to execute Python commands and display results like graphs. Allows to include documentation in Markdown format