def membre(p, q):
for u in q:
if u == p:
return True return False
def intersection(p, q):
resultat = []
for u in p:
if membre(u, q):
resultat.append(u) return resultat
# Q3: len(p)*len(q)
# Q 4
# SELECT idensemble FROM membre JOIN points
# ON points.id = membre.idpoint
# WHERE x = a AND y = b
# Q5
# SELECT x, y FROM points
# JOIN membre AS m1 ON points.id = m1.idpoint
# JOIN membre AS m2 ON points.id = m2.idpoint
# WHERE m1.idensemble = i AND m2.idensemble = j
# Q6
# SELECT idpoint FROM membre AS m1
# JOIN membre as m2 ON m1.idensemble = m2.idensemble
# JOIN points ON m2.idpoint = points.id
# WHERE point.x = a and points.y = b
# Q7: [1,1,2]
# Q8
def bits(x, k):
return (x // 2**k) % 2 def code(n, p):
x, y = p
resultat = []
for k in range(n-1, -1, -1):
u, v = bits(x, k), bits(y, k) resultat.append(2*u + v)
return resultat
# Q9: 000 < 012 < 101 < 233 < 311
1
# Q10
def compare_pcodes(n, c1, c2):
for k in range(n):
if c2[k] > c1[k]:
return 1
if c2[k] < c1[k]:
return -1 return 0
# Q11: [[0,0], [0,3], [1,2], [3,0], [3,1], [3,2], [3,3]]
# Q12: [[0,0], [0,3], [1,2], [3,4]] (compacté).
# Q13
def ksuffixe(n, k, q):
if k == 0 or q[n-k] == 4:
q1 = list(q) q1[n-k-1] = 4 return q1 else:
return q
# Q14
def compacte(n, s):
liste_1 = list(s) for k in range(n):
liste_2 = []
j = 0
while j < len(liste_1):
if j+3 < len(liste_1) and ksuffixe(n, k, liste_1[j]) == ksuffixe(n, k, liste_1[j+3]):
liste_2.append(ksuffixe(n, k, liste_1[j])) j += 4
else:
liste_2.append(liste_1[j]) j += 1
liste_1 = liste_2 return liste_1
# Q 15
def compare_ccodes(n, p, q):
j = 0
while j < n and p[j] == q[j]:
j += 1 if j == n:
return 0 if p[j] == 4:
return -2 if q[j] == 4:
return 2
2
if p[j] < q[j]:
return 1 else:
return -1
# Q 16
def intersection2(n, p, q):
resultat = []
ind1, ind2 = 0, 0
while ind1 < len(p) and ind2 < len(q):
c = compare_ccodes(n, p[ind1], q[ind2]) if c == 0:
resultat.append(p[ind1]) ind1 += 1
ind2 += 1 if c == 1:
ind1 += 1 if c == -1:
ind2 += 1 if c == 2:
resultat.append(p[ind1]) ind1 += 1
if c == -2:
resultat.append(q[ind2]) ind2 += 1
return resultat
s0 = [[0,0], [0,1], [0,2], [0,3], [2,2], [3,0]]
cp0 = compacte(2, s0)
s1 = [[0,0], [0,3], [1,2], [3,0], [3,1], [3,2], [3,3]]
cp1 = compacte(2, s1)
liste_test_2 = [[0,0], [0,1], [1,0], [1,1], [3,0], [2,2]]
codes_2 = [code(2, p) for p in liste_test_2]
print('codes_2 : ', codes_2)
resultat_2 = compacte(2, codes_2) print('résultat_2 : ', resultat_2) print('\n')
liste_test_3 = [[0,0], [1,1], [2,2], [3,3], [0,6], [0,7],
[1,6], [1,7], [2,4], [2,5], [3,4], [3,5], [4,0], [4,1], [5,0], [5,1], [4,2], [4,3], [5,2], [5,3], [6,0], [6,1], [7,0], [7,1], [6,2], [6,3],[7,2], [7,3], [6,6], [6,7], [7,6], [7,7]]
codes_3 = [code(3, p) for p in liste_test_3]
print('codes_3 : ', codes_3) print('\n')
3
resultat_3 = compacte(3, codes_3) print('résultat_3 : ', resultat_3)
4