8000 Aggiunto soluzione con funzioni ricorsive per esercizio 2 e 3 · mathcoding/programming@4b31d22 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit 4b31d22

Browse files
committed
Aggiunto soluzione con funzioni ricorsive per esercizio 2 e 3
1 parent 72c6878 commit 4b31d22

File tree

1 file changed

+37
-9
lines changed

1 file changed

+37
-9
lines changed

scripts/Soluzioni_es_5.py

Lines changed: 37 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -23,13 +23,14 @@ def Equal(As, Bs):
2323

2424
# Soluzione Esercizio 2: intersezione di due tuple
2525
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())
3334

3435
def Intersect(As, Bs):
3536
""" Intersects the elements in As with the elements in Bs.
@@ -45,10 +46,18 @@ def TestZero(n=1):
4546
# Passa alla funzione due tuple
4647
if Intersect((2,3,4,2,1,2,7), (2,3,2,3,4)) == (2, 3, 4):
4748
return 'ok'
48-
else:
49+
if n == 2:
4950
# Passa alla funzione due liste, ritorna una tupla
5051
if Intersect([2,3,4,2,1,2,7], [2,3,2,3,4]) == (2, 3, 4):
5152
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'
5261
return 'failed'
5362

5463
# Soluzione Esercizio 3
@@ -61,6 +70,17 @@ def RimuoviDuplicati(As, Bs):
6170
Rs = Rs + [a]
6271
for r in Rs:
6372
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+
6484
# Per soluzioni esercizio 4 e 5, lezione del 16/11/2017
6585

6686
# Soluzione Esercizio 6
@@ -135,14 +155,22 @@ def ComputeTables(Ls):
135155
print('Bs == Cs:', Equal(Bs,Cs))
136156

137157
# 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))
139162

140163
# Test per esercizio 3
141164
L1 = [2, 4, 2, 5, 6, 6, 3, 2, 9, 4]
142165
L2 = [2, 4, 7]
143166
RimuoviDuplicati(L1, L2)
144167
print('L1 =', L1)
145168

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+
146174
# Test per esercizio 6
147175
TestFrequenze('../data/canzone.txt')
148176
Fs = TestFrequenze('../data/LeAvventureDiPinocchio.txt')

0 commit comments

Comments
 (0)
0