# Slides 2021/22¶

old 2020/21 slides

## Lab A.1¶

Thursday 23 Sep 2021

## Lab A.2¶

Thursday, Sep 30th, 2021

Tools recap

Exercises how to:

• NOTE 1: when I ask you think what a certain code does, write down the answer, don’t just think about it - to pass the exam you must have zero uncertainties about syntax & expressions

• NOTE 2: many times I will ask you to not use if statements, even if that solution would be more elegant & short: the reason is I want you to get familiar with boolean expressions, even if they may look ugly.

Basics

In class: Basics 4 challenge

The rest is left as homework:

Strings

In class: Strings 5 - Challenge

The rest is left as homework:

References

## Lab A.3¶

Friday, Oct 1st, 2021

Lists:

In class: Lists 5 - Challenges

The rest is left as homework:

Tuples:

In class: Tuples 2 - Challenges

The rest is left as homework:

References

EXTRA challenges

Since for now I want you to focus on expressions, first challenges on softpython don’t require loops. Still, if you want, you can generalize them with following extra tasks:

• Lists - Super DUPER sorted: 2) Generalize previous approach so it works with and number of words. For this you will need cycles and/or if statements

• Lists - Toys in the Attic: 2) Generalize the previous approach. Imagine you have an attic which has an unspecified number of categories (here it’s 5 but it could 1000):

attic = [3, 'doll',     'lego',      'minicar',
2, 'frame',    'brushes',
4, 'bike',     'pump',      'racket',  'ball',
2, 'vase',     'fertilizer',
3, 'old chair','lamp deco', 'stool']


and you want to obtain a list of lists like this:

[
['doll','lego','minicar'],
['frame', 'brushes'],
['bike', 'pump', 'racket', 'ball'],
['vase','fertilizer'],
['old chair','lamp deco','stool']
]


(without caring about the variables toys, painting and sports)

• In this case, you can and should use cycles and/or if statements when needed.

## Lab A.4¶

Thursday, Oct 7th, 2021

References

### (sets)¶

Passerini didn’t cover them, if you want have a look at

They may be useful to start, since keys in dictionaries behave much like sets.

### Dictionaries¶

In class: Dictionaries 5 - Challenges

• they always have a fixed number of inputs, but you may wish to generalize them with loops

The rest is left as homework:

### Conditionals¶

In class: if 2: challenge

The rest is left as homework:

References: Andrea Passerini - complex statements

### only for the pros¶

on repl: fill_sides

## Lab A.5¶

Friday, Oct 8th, 2021

### In class:¶

References: Andrea Passerini - complex statements

### At home:¶

for loops

while loops: while 1. intro

sequences: sequences 1. intro

## Lab A.6¶

Thursday, Oct 14th, 2021

### In class:¶

References: Andrea Passerini slides - functions

### At home:¶

Functions:

Matrices as lists:

Mixed structures:

## Lab A.7¶

Friday, Oct 15th, 2021

## Lab A.8¶

Thursday, Oct 21st, 2021

IMPORTANT NOTE:

• today’s lab (Thursday ) will start at 16:30 in b106

• on Friday 22nd there will be no lab

• next week labs restart regularly

## Lab A.9¶

Thursday, Oct 28th, 2021

### Looking at the midterm¶

1. After this lab, consider trying the old Part A exams which don’t display networks.

2. IMPORTANT: there is a References part on SoftPython, where you can find extra resource for Part A of this course. In particular, I recommend doing:

• the explicitly named exercises from Edabit and LeetCode, plus you can find more on HackerRank and Geeks for Geeks

• have a look at Foundations of Python programming book in particular the worked projects

• If you struggle with programming, the stuff above is not optional!

### In class:¶

References:

Andrea Passerini - Numpy and matplotlib

## Lab A.10¶

Friday, Oct 29th, 2021

### In class:¶

References:

Andrea Passerini - Pandas

## Lab A.11¶

Thursday, Nov 4th, 2021

Today we are going to see applications, particular how to display relational data.

Note focus in Part A is just displaying, not exploration algorithms (which we will see in Part B)

First install required libraries (graphviz, pydot, networkx)

If for some reason GraphViz doesn’t work on your system: ask/mail mail me, and in the meanwhile you could use an unholy hack on repl which also uses the online service Gravizo and matplotlib (you can also run it from your laptop). Just run it, wait for the thing to load all the modules, and you should see some graph - notice you might need to properly center it.

### Looking at the midterm¶

After this lab, consider trying the remaining old Part A exams which display networks.

### In class:¶

In class try doing one exercise per type, es:

• matrices: Trichain, Bipartite (from soft import draw_mat)

• networkx: Offshore (from soft import draw_nx)

## Lab B.1¶

Thursday Nov 11th, 2021

New tutoring service

by Gabriele Masina (gabriele.masina (guess what) studenti.unitn.it) has been set up on the following timetable starting from Monday 15 november until Wednesday 15 december (included)

• Mondays: 9:30-11:30 A202

• Wednesdays: 9:30-11:30 A202

Part B Focus

• complex data structures

• perfomance

How:

• big .py files

• debugging

• testing

VSCode:

From now on we will:

• only use Visual Studio Code

• only edit .py files

Btw, if you want you can even load jupyter notebooks (.ipynb) inside VSCode

New testing: In part B we use unittest: see errors and testing on sciprog

• in particular, DO READ TROUBLESHOOTING - you are very likely to encounter problems

Let’s go object oriented: OOP (only first part, next time we will do challenges together)

## Lab B.2¶

Thursday Nov 18th, 2021

### Indexing¶

Have a look at Indexing chapter: probably you will have a hard time understanding the exercises and your solution will look different from proposed ones: try asking yourself why indexing with dictionaries might save computation time

From now on, in general try avoiding:

• in operator on sequences like strings, lists, tuples: it scans the whole sequence!

• search methods (.find, .index, .remove…)

• in particular methods from strings (.replace,etc): PartB is about algorithms, not Python tricks!

• pop(k) (but pop(-1) is fine)

• needlessly using slices, they create new data structures!

## Lab B.3¶

Friday Nov 19th, 2021

worksheets on sciprog:

NOTE: the worksheet is in progress, I will surely add material on multiple recursion

In general, when you don’t know how to tackle a recursive problem, first try the SimpleFP way and then ModAcc

Simple functional programming (SimpleFP)

• immutability

• very limited instructions set

• never re-assign variables

• always create NEW memory regions

Recursion with modifiable accumulators (ModAcc)

• extra accumulators variables

• helper functions

• you can mutate accumulators

References:

## Lab B.4¶

Thursday Nov 25th, 2021

I split recursion into many pages and added stuff, see Recursion 1 - simplefp, Recursion 2 - accumulators and indexes and Recursion 4 - challenges

Today we see:

## Lab B.5¶

Friday Nov 26th, 2021

In lab:

At home:

## Lab B.6¶

Thursday Dec 2nd, 2021

In lab:

• scanning with while

• pointers

• cutting

Please start doing exercises in Part B references

At home:

## Lab B.7¶

Friday Dec 3thrd, 2021

In lab:

Again: Please start doing exercises in Part B references

At home: try finishing whole Queues worksheets

## Lab B.8¶

Thursday Dec 9th, 2021

XI COMMANDMENT: Never ever search keys in a dictionary with looping!

DO NOT write code like this, it’s $$O(n)$$:

for key in d:
if key == 'searched key':
do something ...


USE instead in operator which only takes $$O(1)$$

if 'searched key' in d:
do something


Looping on dictionaries kills performance and in Part B is often a fatal mistake, many people still have this bad habit. Please (re) read softpython - iteration in dictionaries and complexity - dictionaries

Today:

At home: try to finish whole Binary trees worksheet

Yet again: Please start doing exercises in Part B references

## Lab B.9¶

Friday Dec 10th, 2021

Generic trees, at home: try to finish whole Generic trees worksheet

• Further exercise: try doing bintree exercses also on generic trees.