@@ -23,13 +23,14 @@ def Equal(As, Bs):
23
23
24
24
# Soluzione Esercizio 2: intersezione di due tuple
25
25
def IntersectRec (As , Bs ):
26
- """ Intersects the elements in As with the elements in Bs.
27
- Do not count duplicates elements """
28
- Rs = ()
29
- for a in As :
30
- if a in Bs and a not in Rs :
31
- Rs = Rs + (a ,)
32
- return Rs
26
+ def InterI (As , Rs ):
27
+ if As == () or As == []:
28
+ return Rs
29
+ if As [0 ] in Bs and As [0 ] not in Rs :
30
+ return InterI (As [1 :], Rs + (As [0 ],))
31
+ return InterI (As [1 :], Rs )
32
+
33
+ return InterI (As , tuple ())
33
34
34
35
def Intersect (As , Bs ):
35
36
""" Intersects the elements in As with the elements in Bs.
@@ -45,10 +46,18 @@ def TestZero(n=1):
45
46
# Passa alla funzione due tuple
46
47
if Intersect ((2 ,3 ,4 ,2 ,1 ,2 ,7 ), (2 ,3 ,2 ,3 ,4 )) == (2 , 3 , 4 ):
47
48
return 'ok'
48
- else :
49
+ if n == 2 :
49
50
# Passa alla funzione due liste, ritorna una tupla
50
51
if Intersect ([2 ,3 ,4 ,2 ,1 ,2 ,7 ], [2 ,3 ,2 ,3 ,4 ]) == (2 , 3 , 4 ):
51
52
return 'ok'
53
+ if n == 3 :
54
+ # Passa alla funzione due liste, ritorna una tupla
55
+ if IntersectRec ((2 ,3 ,4 ,2 ,1 ,2 ,7 ), (2 ,3 ,2 ,3 ,4 )) == (2 , 3 , 4 ):
56
+ return 'ok'
57
+ if n == 4 :
58
+ # Passa alla funzione due liste, ritorna una tupla
59
+ if IntersectRec ([2 ,3 ,4 ,2 ,1 ,2 ,7 ], [2 ,3 ,2 ,3 ,4 ]) == (2 , 3 , 4 ):
60
+ return 'ok'
52
61
return 'failed'
53
62
54
63
# Soluzione Esercizio 3
@@ -61,6 +70,17 @@ def RimuoviDuplicati(As, Bs):
61
70
Rs = Rs + [a ]
62
71
for r in Rs :
63
72
As .remove (r )
73
+
74
+ def RimuoviDuplicatiRec (As , Bs ):
75
+ def RMI (Ls ):
76
+ if Ls == []:
77
+ return
78
+ if Ls [0 ] in Bs :
79
+ As .remove (Ls [0 ])
80
+ RMI (Ls [1 :])
81
+
82
+ return RMI (As [:])
83
+
64
84
# Per soluzioni esercizio 4 e 5, lezione del 16/11/2017
65
85
66
86
# Soluzione Esercizio 6
@@ -135,14 +155,22 @@ def ComputeTables(Ls):
135
155
print ('Bs == Cs:' , Equal (Bs ,Cs ))
136
156
137
157
# Test per esercizio 3
138
- print ('Test zero: ' + TestZero ())
158
+ print ('Test zero liste: ' + TestZero (1 ))
159
+ print ('Test zero tuple: ' + TestZero (2 ))
160
+ print ('Test zero ricorsiva liste: ' + TestZero (3 ))
161
+ print ('Test zero ricorsiva tuple: ' + TestZero (4 ))
139
162
140
163
# Test per esercizio 3
141
164
L1 = [2 , 4 , 2 , 5 , 6 , 6 , 3 , 2 , 9 , 4 ]
142
165
L2 = [2 , 4 , 7 ]
143
166
RimuoviDuplicati (L1 , L2 )
144
167
print ('L1 =' , L1 )
145
168
169
+ L1 = [2 , 4 , 2 , 5 , 6 , 6 , 3 , 2 , 9 , 4 ]
170
+ L2 = [2 , 4 , 7 ]
171
+ RimuoviDuplicatiRec (L1 , L2 )
172
+ print ('L1 =' , L1 )
173
+
146
174
# Test per esercizio 6
147
175
TestFrequenze ('../data/canzone.txt' )
148
176
Fs = TestFrequenze ('../data/LeAvventureDiPinocchio.txt' )
0 commit comments