Skip to content

Commit cef00c3

Browse files
committed
Soluzioni Esercitazione 4
1 parent 988f08e commit cef00c3

File tree

3 files changed

+415
-33
lines changed

3 files changed

+415
-33
lines changed

scripts/pairslist.py

Lines changed: 100 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,14 @@
55
@author: gualandi
66
"""
77

8-
from pairslist_impl import Head, Tail, EmptyList, MakeList
9-
from random import seed, randint
8+
#from pairslist_impl import Head, Tail, EmptyList, MakeList
9+
from pairslist_array import Head, Tail, EmptyList, MakeList
10+
11+
from random import randint
1012

1113
# Inizializza il generatore di numeri casuali
1214
# Vedi la documentazione del modulo random:
1315
# https://docs.python.org/3/library/random.html
14-
seed(13)
1516
def MakeRandomInts(n, a, b):
1617
""" Restituisce una lista n di numeri causali, uniformente distribuiti
1718
nell'intervallo [a,b] (estremi compresi) """
@@ -57,6 +58,18 @@ def Nth(As, i):
5758
return Head(As)
5859
return Nth(Tail(As), i-1)
5960

61+
def Equal(As, Bs):
62+
""" Check if two lists have the same sequence of elements """
63+
if IsEmpty(As) and IsEmpty(Bs):
64+
return True
65+
if IsEmpty(As) and not IsEmpty(Bs):
66+
return False
67+
if not IsEmpty(As) and IsEmpty(Bs):
68+
return False
69+
if Head(As) != Head(Bs):
70+
return False
71+
return Equal(Tail(As), Tail(Bs))
72+
6073
def Length(As):
6174
""" Restituisce il numero di elementi contenuto nella lista As """
6275
def LengthI(Ls, n):
@@ -109,37 +122,91 @@ def ReverseI(Ls, Rs):
109122
return ReverseI(Tail(Ls), MakeList(Head(Ls), Rs))
110123
return ReverseI(Tail(As), MakeList(Head(As)))
111124

112-
113-
#-----------------------------------------------
114-
# MAIN function per testare tutte le soluzioni
115-
#-----------------------------------------------
116-
if __name__ == "__main__":
117-
Ls = MakeRange(1, 5)
118-
print("MakeList(7): ", end='')
119-
PrintList(Ls)
120-
121-
Rs = MakeRandomInts(10, 1, 100)
122-
print("MakeRandomInts(10, 1, 100): ", end='')
123-
PrintList(Rs)
125+
def Contains(As, value):
126+
""" Controlla se la lista As contiene un elemento pari a 'value' """
127+
if IsEmpty(As):
128+
return False
129+
if Head(As) == value:
130+
return True
131+
return Contains(Tail(As), value)
132+
133+
def Count(As, value):
134+
""" Conta il numero di elementi pari a 'value' nella lista As """
135+
def CountI(Ls, counter):
136+
if IsEmpty(Ls):
137+
return counter
138+
if Head(Ls) == value:
139+
return CountI(Tail(Ls), counter+1)
140+
return CountI(Tail(Ls), counter)
124141

125-
Cs = MakeRange(3,7)
126-
print("MakeRange(3,7): ", end='')
127-
PrintList(Cs)
142+
return CountI(As, 0)
128143

129-
print("Head(Ls):", Head(Ls))
130-
print("Tail(Ls):", Tail(Ls))
131-
132-
print("Nth(Ls, 5):", Nth(Ls, 5))
133-
134-
print("Length(Ls):", Length(Ls))
144+
def RemoveFirst(As, value):
145+
""" Rimuovi il primo elemento di 'value' trovato in As """
146+
if IsEmpty(As):
147+
return As
148+
if Head(As) == value:
149+
return Tail(As)
150+
return MakeList(Head(As), RemoveFirst(Tail(As), value))
135151

136-
Bs = MakeRange(1, 3)
137-
print("Append:", Append(Ls, Bs))
138-
139-
print("Scala:", Scala(Bs, 0.5))
140-
print("Quadrati:", Quadrati(Bs))
141-
142-
print("Map:", Map(lambda x: x**3, Ls))
143-
print("Filter:", Filter(lambda x: x % 2 == 0, Ls))
152+
def RemoveAll(As, value):
153+
""" Rimuovi tutti gli elementi di 'value' trovato in As """
154+
if IsEmpty(As):
155+
return As
156+
if Head(As) == value:
157+
return RemoveAll(Tail(As), value)
158+
return MakeList(Head(As), RemoveAll(Tail(As), value))
159+
160+
def FoldRight(Op, As, z):
161+
""" Riduci la lista As, applicando ad ogni suo elemento la funzione F()
162+
e accumulando i risultati tramite l'operazione Op() """
163+
if IsEmpty(As):
164+
return z
165+
return Op(Head(As), FoldRight(Op, Tail(As), z))
166+
167+
def FoldLeft(Op, As, z):
168+
if IsEmpty(As):
169+
return z
170+
return FoldLeft(Op, Tail(As), Op(Head(As), z))
144171

145-
print("Reverse:", Reverse(Ls))
172+
Fold = FoldLeft
173+
174+
# SOLUZIONI IN TERMINI DI FOLD (Right)
175+
176+
def FoldLength(Ls):
177+
""" Lunghezza di una lista in termini di fold """
178+
return Fold(lambda x,y: 1+y, Ls, 0)
179+
180+
def Sum(As):
181+
""" Calcola la somma di tutti gli elementi nella lista As """
182+
def Add(a, b):
183+
return a+b
184+
return Fold(Add, As, 0)
185+
186+
def Prod(As):
187+
""" Calcola la somma di tutti gli elementi nella lista As """
188+
def Mul(a, b):
189+
return a*b
190+
return Fold(Mul, As, 1)
191+
192+
def Min(As):
193+
""" Più piccolo elemento della lista As """
194+
return Fold(min, Tail(As), Head(As))
195+
196+
def Max(As):
197+
""" Più grande elemento della lista As """
198+
return Fold(max, Tail(As), Head(As))
199+
200+
def FoldMap(F, Ls):
201+
""" Map in termini di Fold """
202+
return Fold(lambda x,y: MakeList(F(x), y), Ls, EmptyList())
203+
204+
def FoldFilter(P, Ls):
205+
""" Filter in termini di Fold """
206+
return Fold(lambda x,y: MakeList(x, y) if P(x) else y, Ls, EmptyList())
207+
208+
def FoldReverse(Ls):
209+
""" Reverse in termini di Fold """
210+
def Concatenate(x, Ls):
211+
return Append(Ls, MakeList(x))
212+
return Fold(Concatenate, Ls, EmptyList())

0 commit comments

Comments
 (0)