Exam - Wed 12, Jan 2022

Scientific Programming - Data Science Master @ University of Trento

Download exercises and solutions

Part A - Prezzario

NOTICE: this part of the exam was ported to softpython website

There you can find a more curated version (notice it may be longer than here)

Open Jupyter and start editing this notebook exam-2022-01-22.ipynb

You are going to analyze the dataset EPPAT-2018-new-compact.csv, which is the price list for all products and services the Autonomous Province of Trento may require. Source: dati.trentino.it

DO NOT WASTE TIME LOOKING AT THE WHOLE DATASET!

The dataset is quite complex, please focus on the few examples we provide

We will show examples with pandas, but it is not required to solve the exercises.

[2]:
import pandas as pd
import numpy as np

pd.set_option('display.max_colwidth', None)
df = pd.read_csv('EPPAT-2018-new-compact.csv', encoding='latin-1')

The dataset contains several columns, but we will consider the following ones:

[3]:
df=df[['Codice Prodotto','Descrizione Breve Prodotto','Categoria','Prezzo']]
df[:22]

../../../_images/exams_2022-01-12_solutions_exam-2022-01-12-sol_5_0.png

Pompa completa a motore Example

If we look at the dataset, in some cases we can spot a pattern (rows 3 to 8 included):

[4]:
df[3:12]
[4]:
Codice Prodotto Descrizione Breve Prodotto Categoria Prezzo
3 A.02.40.0010 POMPA COMPLETA DI MOTORE NaN NaN
4 A.02.40.0010.010 fino a mm 50. Noli e trasporti 2.21
5 A.02.40.0010.020 oltre mm 50 fino a mm 100. Noli e trasporti 3.36
6 A.02.40.0010.030 oltre mm 100 fino a mm 150. Noli e trasporti 4.42
7 A.02.40.0010.040 oltre mm 150 fino a mm 200. Noli e trasporti 5.63
8 A.02.40.0010.050 oltre mm 200. Noli e trasporti 6.84
9 A.02.40.0020 GRUPPO ELETTROGENO NaN NaN
10 A.02.40.0020.010 fino a 10 KW Noli e trasporti 8.77
11 A.02.40.0020.020 oltre 10 fino a 13 KW Noli e trasporti 9.94

We see the first column holds product codes. If two rows share a code prefix, they belong to the same product type. As an example, we can take product A.02.40.0010, which has 'POMPA COMPLETA A MOTORE' as description (‘Descrizione Breve Prodotto’ column). The first row is basically telling us the product type, while the following rows are specifying several products of the same type (notice they all share the A.02.40.0010 prefix code until 'GRUPPO ELETTROGENO' excluded). Each description specifies a range of values for that product: fino a means until to , and oltre means beyond.

Notice that:

  • first row has only one number

  • intermediate rows have two numbers

  • last row of the product series (row 8) has only one number and contains the word oltre ( beyond ) (in some other cases, last row of product series may have two numbers)

A1 extract_bounds

Write a function that given a Descrizione Breve Prodotto as a single string extracts the range contained within as a tuple.

If the string contains only one number n:

  • if it contains UNTIL ( ‘fino’ ) it is considered a first row with bounds (0,n)

  • if it contains BEYOND ( ‘oltre’ ) it is considered a last row with bounds (n, math.inf)

DO NOT use constants like measure units ‘mm’, ‘KW’, etc in the code

Show solution
[5]:
import math

#use this list to rmeove unneeded stuff
PUNCTUATION=[',','-','.','%']
UNTIL = 'fino'
BEYOND = 'oltre'

def extract_bounds(text):
    raise Exception('TODO IMPLEMENT ME !')

assert extract_bounds('fino a mm 50.') == (0,50)
assert extract_bounds('oltre mm 50 fino a mm 100.') == (50,100)
assert extract_bounds('oltre mm 200.') == (200, math.inf)
assert extract_bounds('da diametro 63 mm a diametro 127 mm') == (63, 127)
assert extract_bounds('fino a 10 KW') ==  (0,10)
assert extract_bounds('oltre 156 fino a 184 KW') ==  (156,184)
assert extract_bounds('fino a 170 A, avviamento elettrico') == (0,170)
assert extract_bounds('oltre 170 A fino a 250 A, avviamento elettrico') == (170, 250)
assert extract_bounds('oltre 300 A, avviamento elettrico')  == (300, math.inf)
assert extract_bounds('tetti piani o con bassa pendenza - fino al 10%') == (0,10)
assert extract_bounds('tetti a media pendenza - oltre al 10% e fino al 45%') == (10,45)
assert extract_bounds('tetti ad alta pendenza - oltre al 45%') == (45, math.inf)

A2 extract_product

Write a function that given a filename, a code and a unit, parses the csv until it finds the corresponding code and RETURNS one dictionary with relevant information for that product

  • Prezzo ( price ) must be converted to float

  • implement the parsing with a csv.DictReader, see example

  • as encoding, use latin-1

[6]:
# Suppose we want to get all info about A.02.40.0010 prefix:
df[3:12]
[6]:
Codice Prodotto Descrizione Breve Prodotto Categoria Prezzo
3 A.02.40.0010 POMPA COMPLETA DI MOTORE NaN NaN
4 A.02.40.0010.010 fino a mm 50. Noli e trasporti 2.21
5 A.02.40.0010.020 oltre mm 50 fino a mm 100. Noli e trasporti 3.36
6 A.02.40.0010.030 oltre mm 100 fino a mm 150. Noli e trasporti 4.42
7 A.02.40.0010.040 oltre mm 150 fino a mm 200. Noli e trasporti 5.63
8 A.02.40.0010.050 oltre mm 200. Noli e trasporti 6.84
9 A.02.40.0020 GRUPPO ELETTROGENO NaN NaN
10 A.02.40.0020.010 fino a 10 KW Noli e trasporti 8.77
11 A.02.40.0020.020 oltre 10 fino a 13 KW Noli e trasporti 9.94

A call to

pprint(extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0010', 'mm'))

Must produce:

{'category': 'Noli e trasporti',
 'code': 'A.02.40.0010',
 'description': 'POMPA COMPLETA DI MOTORE',
 'measure_unit': 'mm',
 'models': [{'bounds': (0, 50),        'price': 2.21, 'subcode': '010'},
            {'bounds': (50, 100),      'price': 3.36, 'subcode': '020'},
            {'bounds': (100, 150),     'price': 4.42, 'subcode': '030'},
            {'bounds': (150, 200),     'price': 5.63, 'subcode': '040'},
            {'bounds': (200, math.inf),'price': 6.84, 'subcode': '050'}]}

Notice that if we append subcode to code (with a dot) we obtain the full product code.

Show solution
[7]:

import csv from pprint import pprint def extract_product(filename, code, measure_unit): raise Exception('TODO IMPLEMENT ME !') pprint(extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0010', 'mm')) assert extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0010', 'mm') == \ {'category': 'Noli e trasporti', 'code': 'A.02.40.0010', 'description': 'POMPA COMPLETA DI MOTORE', 'measure_unit': 'mm', 'models': [{'bounds': (0, 50), 'price': 2.21, 'subcode': '010'}, {'bounds': (50, 100), 'price': 3.36, 'subcode': '020'}, {'bounds': (100, 150), 'price': 4.42, 'subcode': '030'}, {'bounds': (150, 200), 'price': 5.63, 'subcode': '040'}, {'bounds': (200, math.inf),'price': 6.84, 'subcode': '050'}]} #pprint(extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0020', 'KW')) #pprint(extract_product('EPPAT-2018-new-compact.csv', 'B.02.10.0042', 'mm')) #pprint(extract_product('EPPAT-2018-new-compact.csv','B.30.10.0010', '%'))

A3 plot_product

Implement following function that takes a dictionary as output by previous extract_product and shows its price ranges.

  • pay attention to display title and axis labels as shown, using input data and not constants.

  • in case last range holds a math.inf, show a > sign

  • if you don’t have a working extract_product, just copy paste data from previous asserts.

>>> extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0010', 'mm')

expected-pompa-a-motore.png

Show solution
[8]:

%matplotlib inline import numpy as np import matplotlib.pyplot as plt def plot_product(product): raise Exception('TODO IMPLEMENT ME !') product = extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0010', 'mm') #product = extract_product('EPPAT-2018-new-compact.csv', 'A.02.40.0020', 'KW') #product = extract_product('EPPAT-2018-new-compact.csv', 'B.02.10.0042', 'mm') #product = extract_product('EPPAT-2018-new-compact.csv','B.30.10.0010', '%') plot_product(product)

Part B

  • Open Visual Studio Code and start editing the folder on your desktop

  • For running tests: open Accessories -> Terminal

B1 Theory

Write the solution in separate ``theory.txt`` file

B1.1

Given a list L of \(n\) elements, please compute the asymptotic computational complexity of the myfun function, explaining your reasoning.

[9]:
def myfun(L):
    n = len(L)
    sums = {}
    for i in range(n):
        sums[i] = 0
        for j in range(i):
            sums[i] += j
    for k in range(n):
        sums[k] += k
    return sums

B1.2

Describe the differences between the Depth-First and the Breadth-First Search algorithms for visiting graphs.

Then, apply BFS to the graph below (write down the visit order only).

graph.png

B2 find_couple

Implement following find_couple method.

def find_couple(self, a, b):
    """ Search the list for the first two consecutive elements having data
        equal to provided a and b, respectively. If such elements are found,
        the position of the first one is returned, otherwise raises LookupError.

        - MUST run in O(n), where n is the size of the list.
        - Returned index start from 0 included
    """

Testing: python3 -m unittest linked_list_test.FindCoupleTest

B3 swap

Implement the method swap:

def swap(self, i, j):
    """ Swap the data of nodes at index i and j. Indeces start from 0 included.
        If any of the indeces is out of bounds, rises IndexError.

        NOTE: You MUST implement this function with a single scan of the list.
    """

Testing: python3 -m unittest linked_list_test.SwapTest

[ ]: