Facebook
From Ample Penguin, 2 Years ago, written in Plain Text.
Embed
Download Paste or View Raw
Hits: 97
  1. def iota (n): # fonction qui fait une liste de taille n-1 (n est un entier)
  2.     tab = []
  3.     for i in range(n):
  4.         tab.append(i)
  5.     return (tab)
  6.  
  7. def contient (tab, n): # fonction qui cherche si l'entier n est dedans
  8.     for i in range (len(tab)):
  9.         if tab[i] == n:
  10.             return True
  11.     return False
  12.     print (contient)
  13.    
  14. def ajouter (tab, x): # valeur a rajouter
  15.     if tab == []:
  16.         return [x]
  17.     else:
  18.         for i in range (len(tab)):
  19.             if contient(tab, x) == False:
  20.                 tab.append(x)
  21.            
  22.     return tab
  23.  
  24. def retirer (tab, x):
  25.     for i in range (len(tab)):
  26.         if contient(tab, x) == True:
  27.             tab.remove(x)
  28.            
  29.     return tab
  30. def voisins(x, y, nX, nY):
  31.      
  32.     N = x + y * nX - nX
  33.     O = x + y * nX - 1
  34.     S = x + y * nX + nX
  35.     E = x + y * nX + 1
  36.  
  37.     if x == 0 and y == 0: # coin gauche haut
  38.         voisin = [S, E]
  39.        
  40.     elif x == (nX - 1) and y == 0: # coin droite haut
  41.         voisin = [O, S]
  42.  
  43.     elif x == 0 and y == (nY - 1): # coin gauche bas
  44.         voisin = [N, E]
  45.        
  46.     elif x == (nX - 1) and y == (nY - 1): # coin droite bas
  47.         voisin = [N, O]
  48.      
  49.     elif y == 0 and (x != 0 or x != (nX - 1)): # premiere rangee
  50.         voisin = [O, S, E]
  51.    
  52.     elif x == 0 and (y != 0 or y != (nY - 1)): # premiere colonne
  53.         voisin = [N, S, E]
  54.        
  55.     elif y == (nY-1) and (x != 0 or x != nX - 1): # derniere rangee
  56.         voisin = [N, O, E]
  57.        
  58.     elif x == (nX-1) and (y != 0 or y != nY - 1): # derniere colonne
  59.         voisin = [N, O, S]
  60.        
  61.     else:       # le reste
  62.         voisin = [N, O, S, E]
  63.    
  64.    
  65.     return voisin
  66.    
  67. def testFinaux():
  68.     assert iota(0) == [], 'tableau vide'
  69.     assert iota(5) == [0,1,2,3,4]
  70.     assert iota(10) == [0,1,2,3,4,5,6,7,8,9]
  71.     assert iota(15) == [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14]
  72.     assert iota(22) ==[0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21]
  73.     assert contient([], 0) == False
  74.     assert contient([100,1], 10) == False
  75.     assert contient([1,2,3,4,5], 5) == True
  76.     assert contient([5,5,5,5,5], 5) == True
  77.     assert contient ([],10) == False    
  78.     assert ajouter([], 4) == [4]
  79.     assert ajouter([9,2,5], 4) == [9,2,5,4]
  80.     assert ajouter([9,2,5], 2) == [9,2,5]
  81.     assert ajouter([20,5], 2) == [20,5,2]
  82.     assert ajouter([7,8], 0) == [7,8,0]
  83.     assert ajouter([0], 0) == [0]
  84.     assert ajouter([0], 1) == [0,1]
  85.     assert retirer([9,2,5], 2) == [9,5]
  86.     assert retirer([9,2,5], 4) == [9,2,5]
  87.     assert retirer([], 2) == []
  88.     assert retirer([2], 2) == []
  89.     assert retirer([1,1,6], 1) == [6]
  90.     assert voisins (2,1,8,4) == [2, 9, 18, 11]
  91.     assert voisins (5,2,8,4) == [13, 20, 29, 22]
  92.     assert voisins (0,0,8,4) == [8, 1]
  93.     assert voisins (7,3,8,4) == [23, 30]
  94.     assert voisins (7,0,8,4) == [6, 15]
  95.     assert voisins (0,3,8,4) == [16, 25]
  96.     assert voisins (3,0,8,4) == [2, 11, 4]
  97.     assert voisins (0,1,8,4) == [0, 16, 9]
  98.     assert voisins (3,3,8,4) == [19, 26, 28]
  99.     assert voisins (7,2,8,4) == [15, 22, 31]    
  100.    
  101.  
  102.  
  103. testFinaux()
  104.