Docsity

Controle d'informatique, Examens de Informatique

Toggle sidebar
bg1
Lycée MICHELET, classes de PC∗et PSI Année 2020-2021
INFORMATIQUE
Corrigé du TD no3
Q1
✞ ☎
4def suivant (T,i) :
5T[i][0]=1
6T[i] [ i]=1
7for jin range ( 1 ,i) :
8T[i] [ j]= T[i-1][j-1]+ T[i-1][j]
✝ ✆
Q2
✞ ☎
11 def Pascal (n) :
12 T=[[0 for jin range (n) ] for iin range (n) ]
13 T[ 0 ] [ 0 ] = 1
14 #initialisation
15 for iin range ( 1 ,n) :
16 suivant (T,i)
17 return T
✝ ✆
Q3
✞ ☎
20 def grille (n) :
21 M= [ ]
22 for iin range (n) :
23 L= [ ]
24 for jin range (n) :
25 L.append (0)
26 M.append (L)
27 return M
✝ ✆
Q4
✞ ☎
30 def init (n) :
31 G=grille (n)
32 i=random .randrange (n)
33 j=random .randrange (n)
34 G[i] [ j]=1
35 return G
✝ ✆
Q5
✞ ☎
38 def nombre (G,x) :
39 n=len(G)
40 compteur=0
41 for iin range (n) :
42 for jin range (n) :
43 if G[i] [ j]==x:
44 compteur=compteur+1
45 return compteur
✝ ✆
6
pf3
pf4

Aperçu partiel du texte

Télécharge Controle d'informatique et plus Examens au format PDF de Informatique sur Docsity uniquement!

Lycée M ICHELET , classes de PC∗^ et PSI Année 2020-

I NFORMATIQUE

Corrigé du TD no^3

Q

4 def suivant ( T , i ) :

5 T [ i ] [ 0 ] = 1

6 T [ i ] [ i ]=

7 for j in range ( 1 , i ) :

8 T [ i ] [ j ]= T [ i - 1 ] [ j - 1]+ T [ i - 1 ] [ j ]

Q

11 def Pascal ( n ) :

12 T = [ [ 0 for j in range ( n ) ] for i in range ( n ) ]

13 T [ 0 ] [ 0 ] = 1

14 #initialisation

15 for i in range ( 1 , n ) :

16 suivant ( T , i )

17 return T

Q3 ✞ ☎

20 def grille ( n ) :

21 M = [ ]

22 for i in range ( n ) :

23 L = [ ]

24 for j in range ( n ) :

25 L. append ( 0 )

26 M. append ( L )

27 return M

Q

30 def init ( n ) :

31 G = grille ( n )

32 i = random. randrange ( n )

33 j = random. randrange ( n )

34 G [ i ] [ j ]=

35 return G

Q

38 def nombre ( G , x ) :

39 n = len ( G )

40 compteur =

41 for i in range ( n ) :

42 for j in range ( n ) :

43 if G [ i ] [ j ]== x :

44 compteur = compteur +

45 return compteur

Q

48 def compte ( G ) :

49 L = [ ]

50 for i in range ( 4 ) :

51 x = nombre ( G , i )

52 L. append ( x )

53 return L

Q7 La fonction est_exposee renvoie un booléen (True ou False).

Q8 Un produit est nul lorsque l’un des facteurs est nul, donc

(G[i][j]-1)...(G[ii][jj]-1)==0 vaut True dès que l’une des cases concernées vaut 1 ( i.e. est

infectée). Le nombre de cases voisines à tester dépend de l’emplacement de la case par rapport aux bords. Dans la fontion proposée, les quatre premiers cas concernent les bords ; les deux cas suivants les bords supérieur et inférieur ; les deux suivants les bords gauche et droit ; et le dernier cas est le cas général des cases qui ne sont pas au bord.

✞ ☎

59 def est_exposee ( G , i , j ) :

60 n = len ( G )

61 if i ==0 and j ==0:

62 return ( G [ 0 ] [ 1 ] - 1) * ( G [ 1 ] [ 1 ] - 1) * ( G [ 1 ] [ 0 ] - 1)==

63 elif i ==0 and j == n - 1 :

64 return ( G [ 0 ] [ n - 2] - 1) * ( G [ 1 ] [ n - 2] - 1) * ( G [ 1 ] [ n - 1] - 1)==

65 elif i == n - 1 and j ==0:

66 return ( G [ n - 1 ] [ 1 ] - 1) * ( G [ n - 2 ] [ 1 ] - 1) * ( G [ n - 2 ] [ 0 ] - 1) ==

67 elif i == n - 1 and j == n - 1 :

68 return ( G [ n - 1 ] [ n - 2] - 1) * ( G [ n - 2 ] [ n - 2] - 1) * ( G [ n - 2 ] [ n - 1] - 1)==

69 elif i ==0:

70 #ligne 12 :

71 return ( G [ 0 ] [ j - 1] - 1) * ( G [ 1 ] [ j - 1] - 1) * ( G [ 1 ] [ j ] - 1) * ( G [ 1 ] [ j +1] - 1) * ( G [ 0 ] [ j

+1] - 1) ==

72 elif i == n - 1 :

73 return ( G [ n - 1 ] [ j - 1] - 1) * ( G [ n - 2 ] [ j - 1] - 1) * ( G [ n - 2 ] [ j ] - 1) * ( G [ n - 2 ] [ j +1] - 1)

* (^ G^ [^ n^ - 1 ] [^ j^ +1] - 1)==

74 elif j ==0:

75 return ( G [ i - 1 ] [ 1 ] - 1) * ( G [ i - 1 ] [ 1 ] - 1) * ( G [ i ] [ 1 ] - 1) * ( G [ i + 1 ] [ 1 ] - 1) * ( G [ i

+ 1 ] [ 0 ] - 1)==

76 elif j == n - 1 :

77 return ( G [ i - 1 ] [ n - 1] - 1) * ( G [ i - - 1 ] [ n - 2] - 1) * ( G [ i ] [ n - 2] - 1) * ( G [ i + 1 ] [ n - 2] - 1)

* (^ G^ [^ i^ + 1 ] [^ n^ - 1] - 1)==

78 else :

79 #ligne 20 :

80 return ( G [ i - 1 ] [ j - 1] - 1) * ( G [ i - 1 ] [ j ] - 1) * ( G [ i - 1 ] [ j +1] - 1) * ( G [ i ] [ j - 1] - 1) * ( G

[ i ] [ j +1] - 1) * ( G [ i + 1 ] [ j - 1] - 1) * ( G [ i + 1 ] [ j ] - 1) * ( G [ i + 1 ] [ j +1] - 1)==

Q

83 def bernoulli ( p ) :

84 x = random. random ( )

85 if x <= p :

86 return 1

87 else :

88 return 0

142 while fin - debut >2:

143 milieu =( debut + fin ) //

144 if moyenne [ milieu ] > seuil :

145 fin = milieu

146 else :

147 debut = milieu

148 res = milieu / N

149 #affichage

150 plt. plot ( [ 0 , res , res ] , [ seuil , seuil , 0 ] ) #tracé du seuil

151 plt. show ( )

152 return res

calculSeuil(15,40,40,0.1,0.5)