TD 4 - Tableaux 2D
I - Listes compréhensives
•Le tableau [1,-1,1,-1,1,-1,...] faisant 20 cases : [(-1)**k for k in range(20)]
•Un tableau contenant les 20 premiers termes de la suite 1 1 +n
n∈N∗
: [1/(1+n) for n in range(1,21)]
•Un tableau contenant les entiers de 1 à 100 multiples de 7 ou nissant par 7 : [k for k in range(1,101) if k%7==0 or k%10==7]
•Un tableau contenant les entiers de 1 à 100 sauf les multiples de 7 et les nombres nissant par 7.
[k for k in range(1,101) if k%7 !=0 and k%10 !=7]
•Un tableau de 3 lignes et 2 colonnes tel que sa représentation matricielle soit la suivante :
1 2 3 4 5 6
.
[ [1,2] , [3,4] , [5,6] ]
•Un tableau dont la représentation matricielle est la suivante :
1 2 3 4 5 6
2 3 4 5 6 7
3 4 5 6 7 8
4 5 6 7 8 9
5 6 7 8 9 10
. [ [i+j+1 for j in range(6)] for i in range(5) ]
II - Tableaux 2D : Algorithmes 1. Coecients binomiaux
C = [ [0]*11 for i in range(10) ] C[0][0]=1
for n in range(1,10) : for p in range(10) :
if (p==0) : C[n][p] = 1
else : C[n][p] = C[n-1][p-1] + C[n-1][p]
for x in C : print(x)
2. Défoncer une ligne from random import *
a = [ [1]*10 for i in range(10) ] sum = 10
NbCoups = 0 while sum>0 :
line = randint(0,9) a[line][randint(0,9)] = 0 sum = 0
for k in a[line] : sum += k NbCoups += 1
print(NbCoups)
3. Minimum local
alt = [ [i*i + i*j - 60*i + 1.2*j*j - 90*j + 2180 for j in range(100) ] for i in range(100) ] def Deplace(i,j):
for k in range(-1,2):
for m in range(-1,2):
if alt[i+k][j+m]<alt[i][j] : return( [i+k , j+m] ) return(-1)
Pos = [50,50]
while Deplace(Pos[0], Pos[1]) != -1:
Pos = Deplace(Pos[0], Pos[1])
# print(Pos , alt[Pos[0]][Pos[1]]) # si on veut voir le trajet print (Pos) # affiche [14,32]
1
4. Le bébé fou sur l'échiquier TI = 8
TJ = 13
a = [ [ 0 for j in range(TJ) ] for i in range(TI) ] i = 0 # position du fou (numéro de ligne)
j = 0 # position du fou (numéro de colonne) vi = 1 # vitesse selon les lignes
vj = 1 # vitesse selon les colonnes a[0][0] = 1
while a[i][j] != 3:
i = i + vi j = j + vj
if i==0 or i==TI-1 : vi = -vi if j==0 or j==TJ-1 : vj = -vj a[i][j] = a[i][j] + 1
print(i,j) for x in a : print(x) # pour afficher le tableau
C'est la case de coordonnées (1,11) que le bébé-fou visite pour la troisième fois en premier.
5. Plus grand rectangle blanc
a = [ [0 for x in range(20)] for y in range(20)]
for k in range(20) : a[0][k] = 1 a[k][0] = 1 a[19][k] = 1 a[k][19] = 1 a[3] [7] = 1 a[10][10] = 1 a[5] [17] = 1 a[15][3] = 1
#---- Cette fonction teste si le rectangle dont les coins sont
#---- (x1,y1) et (x2,y2) est tout blanc def ToutBlanc(x1,y1, x2,y2):
for x in range(x1,x2+1) : for y in range(y1,y2+1) :
if a[x][y]!=0 : return(False) return(True)
#---- Le programme principal va tester tous les rectangles, et
#---- retenir celui d'aire maximale ch = 0
for x1 in range(20):
for y1 in range(20):
for x2 in range(x1,20):
for y2 in range(y1,20):
if (ToutBlanc(x1,y1, x2,y2)):
aire = (x2-x1+1)*(y2-y1+1) if aire > ch :
ch = aire print(ch)
L'aire maximale est 120 et elle est obtenue pour le rectangle dont les coins sont (11,4) et (18,18)
2
6. La chasse au trésor
terrain = [ [3,2,6] , [4,1,4] , [0,5,2] ] n = len(terrain)
butin = [ [0]*n for i in range(3) ] butin[0] = terrain[0]
for k in range(1,n) :
butin[0][k] = butin[0][k-1]+terrain[0][k]
butin[k][0] = butin[k-1][0]+terrain[k][0]
#--- Calcul du tableau "butin"
def max(a,b) :
if a>b : return a return b
for i in range(1,n) : for j in range(1,n) :
butin[i][j] = max(butin[i-1][j] + terrain[i][j] , butin[i][j-1] + terrain[i][j])
#--- Calcul du chemin
(i,j) = (n-1,n-1) # on part de la fin pour calculer le chemin chemin = [0]*(2*n-2) # le chemin fera 2n-2 déplacements
k = 2*n-3 # case du chemin que l'on remplit : on part de la fin while not (i,j)==(0,0) :
if (i==0) :
j-=1chemin[k] = "droite"
elif (j==0) :
i-=1chemin[k] = "bas"
elif butin[i-1][j]>butin[i][j-1] : i-=1chemin[k] = "bas"
else :
j-=1chemin[k] = "droite"
k-=1
for x in chemin : print(x)
3
7. Puissance 4
def Puiss4(a): # le tableau a contient "0" si la case est vide
ty = len(a) # et "1" ou "2" si le joueur 1 ou 2 a joué dans la case.
tx = len(a[0])
# check les verticales for x in range(tx):
for y in range(ty-3):
if a[y][x]>0 : nbAlign=1
while (nbAlign<=3 and a[y+nbAlign][x]==a[y][x]) : nbAlign += 1
if nbAlign==4 : return a[y][x]
# check les horizontales for x in range(tx-3):
for y in range(ty):
if a[y][x]>0 : nbAlign=1
while (nbAlign<=3 and a[y][x+nbAlign]==a[y][x]) : nbAlign += 1
if nbAlign==4 : return a[y][x]
# check les diagonales vers le bas droit for x in range(tx-3):
for y in range(ty-3):
if a[y][x]>0 : nbAlign=1
while (nbAlign<=3 and a[y+nbAlign][x+nbAlign]==a[y][x]) : nbAlign += 1
if nbAlign==4 : return a[y][x]
# check les diagonales vers le bas gauche for x in range(3,tx):
for y in range(ty-3):
if a[y][x]>0 : nbAlign=1
while (nbAlign<=3 and a[y+nbAlign][x-nbAlign]==a[y][x]) : nbAlign += 1
if nbAlign==4 : return a[y][x]
return(0)
from random import * def RandGame():
a = [[0]*7 for i in range(6)]
for k in range(42) : # on a priori 42 pièces à mettre col = randint(0,6)
while a[0][col]!=0 : col = randint(0,6) # on trouve une colonne non remplie lgn = 0
while lgn<6 and a[lgn][col]==0 : lgn+=1 # on fait tomber la pièce dans la colonne lgn-=1
a[lgn][col] = (k%2) +1 P4 = Puiss4(a)
if P4 : return P4 return 0
S = [0,0,0]
for x in range(1000) : S[RandGame()] += 1 print( S )
# ça affiche par exemple [3, 542, 455]
4