Skip to content

Commit 5ca6eb3

Browse files
committed
Soluzione esercitazione 6
1 parent b37fb2b commit 5ca6eb3

File tree

3 files changed

+139
-2
lines changed

3 files changed

+139
-2
lines changed

latex/esercitazioni/es_6.pdf

3 Bytes
Binary file not shown.

latex/esercitazioni/es_6.tex

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ \section*{}
100100
\mybox{15}{2.75}
101101

102102
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
103-
\item Scrivere una funzione {\tt Insert(As, z, Cmp=lambda x,y: x<y)} che inserisce nella lista {\tt As} l'elemento {\tt value},
103+
\item Scrivere una funzione {\tt Insert(As, z, Cmp=lambda x,y: x<y)} che inserisce nella lista (già ordinata)
104+
{\tt As} l'elemento {\tt value},
104105
rispettando le seguenti regole: se l'elemento è già presente nella lista non viene aggiunto,
105106
altrimenti l'elemento viene aggiunto tra due valori 'x' e 'y' di {\tt As} in modo tale che
106107
valga la relazione $x < z < y$, o più in generale {\tt Cmp(x,z) == True} e {\tt Cmp(z,y) == True}.
@@ -127,7 +128,7 @@ \section*{}
127128

128129
\begin{enumerate}
129130
\item {\bf DotProduct}: prodotto tra due vettori, componente per componente. Dati due vettori $x$ e $y$ in $\mathbb{R}^n$
130-
calcolare lo scalara $p=\sum_{i = 1,\dots,n} x_i y_i$.
131+
calcolare lo scalare $p=\sum_{i = 1,\dots,n} x_i y_i$.
131132
\item {\bf MatrixVector}: prodotto tra matrice e vettore. Data una matrice $A$ di dimensione $m \times n$ e un vettore $x$ in $\mathbb{R}^n$ calcolare il vettore $t$, in cui $t_i=\sum_{j = 1,\dots,n} a_{ij} x_j$.
132133
\item {\bf MatrixMatrix}: prodotto tra due matrici. Date le due matrici $A$ di dimensione $m \times n$ e $B$ di dimensione $n \times m$ calcolare la matrice $P$, in cui $p_{ij}=\sum_{k = 1,\dots,n} a_{ik} b_{kj}$.
133134
\item {\bf Transpose}: matrice trasposta. Data la matrice $A$ di dimensione $m \times n$ calcolare la sua trasposta,

scripts/Soluzioni_es_6.py

Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
# -*- coding: utf-8 -*-
2+
"""
3+
Soluzioni Esercitazione 5
4+
5+
Created on Tue Nov 28 20:57:35 2017
6+
7+
@author: gualandi
8+
"""
9+
10+
# Esercizio 1
11+
def IsSet(Ls, P=lambda x,y: x == y):
12+
def InnerCheck(a, Bs):
13+
for b in Bs:
14+
if P(a, b):
15+
return False
16+
return True
17+
18+
def OuterCheck(As):
19+
if As == []:
20+
return True
21+
if not InnerCheck(As[0], As[1:]):
22+
return False
23+
return OuterCheck(As[1:])
24+
25+
return OuterCheck(Ls)
26+
27+
# Soluzione alternativa
28+
def IsSetAlt(Ls, P=lambda x,y: x == y):
29+
for i,a in enumerate(Ls):
30+
for b in Ls[i+1:]:
31+
if P(a,b):
32+
return False
33+
return True
34+
35+
36+
# Esercizio 2
37+
def Insert(As, value, i):
38+
# L'esercizio si poteva risolvere studiando la documentazione di Python
39+
As.insert(i, value)
40+
# NOTA: in questo caso però veniva modificata la lista passata in input
41+
42+
def InsertAlt(As, value, i):
43+
# In questo caso viene creata una nuova lista, con una copia di tutti gli
44+
# elementi, più l'inserimento dell'elemento da aggiungere
45+
return As[:i] + [value] + As[i:]
46+
47+
48+
# Esercizio 3
49+
def InsertOrder(As, z, Cmp=lambda x,y: x < y):
50+
if Cmp(z, As[0]):
51+
return [z] + As
52+
if Cmp(As[-1], z):
53+
return As + [z]
54+
i = 1
55+
for a,b in zip(As[:-1], As[1:]):
56+
if Cmp(a,z) and Cmp(z,b):
57+
return As[:i] + [z] + As[i:]
58+
i = i + 1
59+
return As
60+
61+
62+
# Esercizio 4
63+
def Contains(As, z):
64+
a = 0
65+
b = len(As)-1
66+
mid = a+(b-a)//2
67+
68+
while a < mid and mid < b:
69+
if As[mid] == z:
70+
return True
71+
if As[mid] < z:
72+
a = mid
73+
else:
74+
b = mid
75+
mid = a+(b-a)//2
76+
77+
return As[a] == z or As[b] == z
78+
79+
80+
# Esercizio 5
81+
from functools import reduce
82+
def DotProduct(As, Bs):
83+
# NOTA: reduce è equivalente a un FoldRight
84+
return reduce(lambda x,y: y[0]*y[1]+x, zip(As,Bs), 0)
85+
86+
def MatrixVector(A, v):
87+
return list(map(lambda x: DotProduct(x,v), A))
88+
89+
def MatrixMatrix(A, B):
90+
return [MatrixVector(A,v) for v in B]
91+
92+
def Transpose(A):
93+
if A[0] == []:
94+
return []
95+
return [list(map(lambda x: x[0], A))] + Transpose(list(map(lambda x: x[1:], A)))
96+
97+
98+
#-----------------------------------------------
99+
# MAIN function per testare tutte le soluzioni
100+
#-----------------------------------------------
101+
if __name__ == "__main__":
102+
# Test per esercizio 1
103+
print("IsSet([2,3,6,4,2]): ", IsSet([2,3,6,4,2]))
104+
print("IsSet([2,3,7,4,5]): ", IsSet([2,3,7,4,5]))
105+
106+
print("IsSetAlt([2,3,6,4,2]): ", IsSetAlt([2,3,6,4,2]))
107+
print("IsSetAlt([2,3,7,4,5]): ", IsSetAlt([2,3,7,4,5]))
108+
print()
109+
110+
# Test per esercizio 2
111+
Ls = [0,1,2,3,5]
112+
print("Ls prima:", Ls)
113+
Insert(Ls, 9, 3)
114+
print("Ls dopo:", Ls)
115+
Bs = InsertAlt(Ls, 9, 3)
116+
print("Soluzione alternativa:", Bs)
117+
print("Oggetti diversi: {}, {}".format(id(Ls), id(Bs)))
118+
print()
119+
120+
# Test per esercizio 3
121+
Cs = sorted(Ls)
122+
print("Test insert ordinato:", InsertOrder(Cs, 7))
123+
print("Test insert ordinato:", InsertOrder(Cs, 2))
124+
print("Test insert ordinato:", InsertOrder(Cs, 4))
125+
print("Test insert ordinato:", InsertOrder(Cs, -2))
126+
127+
# Test per esercizio 4
128+
print("Contains: ", Contains([1,3,4,6,7,9,10,14,15,23], 12))
129+
print("Contains: ", Contains([1,3], 3))
130+
print("Contains: ", Contains([1,3,4,6,7,9,10,14,15,23], 4))
131+
132+
# Test per Eserczio 5
133+
print(DotProduct([1,2,3],[3,2,1]))
134+
print(MatrixVector([[1,2,3],[1,0,1]], [2,1,1]))
135+
print(MatrixMatrix([[1,2,3],[1,0,1]], [[2,1,1],[0,0,1]]))
136+
print(Transpose([[1,2,3],[1,0,1]]))

0 commit comments

Comments
 (0)