# Initiation à la programmation ENSAE 1A

## ecrit_2006.tex

File: ecrit_2006.tex, line 28

```
l = [ 0,1,2,3,4,6,5,8,9,10]
res = True
for i in range (1,len (l)) :
if l[i-1] > l[i] :
res = False

```

File: ecrit_2006.tex, line 58

```
def somme (n) :
return sum ( [ int (c) for c in str (n) ] )

```

File: ecrit_2006.tex, line 66

```
def somme (n) :
l = str (n)           # il ne faut pas confondre l=str (n) avec l = "n"
s = 0
for c in l :          # ou   for i in range (0, len (c)) :
s += int (c)      # ou       s += int (c [i])
return s

```

File: ecrit_2006.tex, line 78

```
def somme (n) :
s = 0
while n > 0 :
s += n % 10
n /= 10    # ici, c'est une division entière, si vous n'êtes pas sûr :
#       n = int (n/10)
return n

```

File: ecrit_2006.tex, line 91

```
def somme (n) :
if n <= 0 : return 0
else : return (n % 10) + somme ( n / 10 )

```

File: ecrit_2006.tex, line 100

```
import math
def somme (n) :
k = int (math.log (n) / math.log (10) + 1)
s = 0
for i in range (1,k+1) :
d = 10 ** i   # ou encore d = int (exp ( k * log (10) ) )
c = n / d
e = n - c * d
f = e / (d / 10)
s += f
return s

```

File: ecrit_2006.tex, line 128

```
n = 0
for i in range (0,10) :
if (n + i) % 3 == 0 :
n += 1

```

File: ecrit_2006.tex, line 177

```
def grenouille (n) :
if   n == 2 : return 2
elif n == 1 : return 1
else :        return grenouille (n-1) + grenouille (n-2)
print grenouille (13)

```

File: ecrit_2006.tex, line 191

```
def grenouille (fin, n = 2, u1 = 1, u2 = 2) :
if   fin == 1   : return u1
elif fin == 2   : return u2
elif n   == fin : return u2
u = u1 + u2
return grenouille (fin, n+1, u2, u)
print grenouille (13)

```

File: ecrit_2006.tex, line 205

```
def grenouille (n) :
if n == 1 : return 1
u1 = 1
u2 = 2
for i in range (3,n+1) :
u  = u1 + u2   # il est impossible de
u1 = u2        # résumer ces trois lignes
u2 = u         # en deux
return u2
print grenouille (13)

```

File: ecrit_2006.tex, line 222

```
def grenouille (n) :
if n == 1 : return 1
u = [1,2]
for i in range (2,n) :
u.append (u [i-1] + u [i-2])
return u [n-1]
print grenouille (12)

```

File: ecrit_2006.tex, line 236

```
def grenouille (n) :
if n == 1 : return 1
u = range (0, n)  # il ne faut pas oublier de créer le tableau
# avec autant de cases que nécessaire
# ici 13
u [0] = 1
u [1] = 2
for i in range (2,n) :
u [i] = u [i-1] + u [i-2]
return u [n-1]
print grenouille (12)

```

File: ecrit_2006.tex, line 261

```
a = "abcdefghijklmnopqrstuvwxyz"
print len (a)
d = {}
for i in range (0,len (a)) :
d [ a [ i ] ] = i

print d ["M"]

```

File: ecrit_2006.tex, line 274

```
examen.py:14:  KeyError: 'M'

```

File: ecrit_2006.tex, line 288

```
a = "abcdefghijklmnopqrstuvwxyz"
print len (a)
d = {}
for i in range (0,len (a)) :
d [ a [ i ] ] = i

print d ["m"]         ######   ligne modifiée

```

File: ecrit_2006.tex, line 301

```
a = "abcdefghijklmnopqrstuvwxyz"
a = a.upper ()        #######   ligne ajoutée
print len (a)
d = {}
for i in range (0,len (a)) :
d [ a [ i ] ] = i

print d ["M"]

```

File: ecrit_2006.tex, line 324

```
def somme (tab) :
l = tab[0]
for i in range (1, len (tab)) :
l += tab [i]
return l
ens = [[0,1],[2,3]]
print somme ( ens )   # affiche [0,1,2,3]
print ens             # affiche [ [0,1,2,3], [2,3] ]

```

File: ecrit_2006.tex, line 345

```
import copy                      ###### ligne ajoutée
def somme (tab) :
l = copy.copy (tab[0])       ###### ligne modifiée
for i in range (1, len (tab)) :
l += tab [i]
return l
ens = [[0,1],[2,3]]
print somme ( ens )   # affiche [0,1,2,3]
print ens             # affiche [ [0,1,2,3], [2,3] ]

```

File: ecrit_2006.tex, line 360

```
def somme (tab) :
l = []                            ###### ligne modifiée
for i in range (0, len (tab)) :   ###### ligne modifiée
l += tab [i]
return l
ens = [[0,1],[2,3]]
print somme ( ens )   # affiche [0,1,2,3]
print ens             # affiche [ [0,1,2,3], [2,3] ]

```

File: ecrit_2006.tex, line 382

```
li  = range (0,10)
sup = [0,9]
for i in sup :
del li [i]
print li

```

File: ecrit_2006.tex, line 393

```
examen.py:44:  IndexError: list assignment index out of range

```

File: ecrit_2006.tex, line 406

```
[ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

```

File: ecrit_2006.tex, line 410

```
[ 1, 2, 3, 4, 5, 6, 7, 8, 9]

```

File: ecrit_2006.tex, line 416

```
li  = range (0,10)
sup = [9,0]      ####### ligne modifiée
for i in sup :
del li [i]
print li

```

File: ecrit_2006.tex, line 439

```
l = ["un", "deux", "trois", "quatre", "cinq"]
for i in range (0,len (l)) :
mi = i
for j in range (i, len (l)) :
if l[mi] < l [j] : mi = j
e      = l [i]
l [mi] = l [i]
l [i]  = e
print l

```

File: ecrit_2006.tex, line 455

```
['un', 'deux', 'deux', 'deux', 'cinq']

```

File: ecrit_2006.tex, line 470

```
l = ["un", "deux", "trois", "quatre", "cinq"]
for i in range (0,len (l)) :
mi = i
for j in range (i, len (l)) :
if l[mi] < l [j] : mi = j
e      = l [mi]       ######## ligne modifiée
l [mi] = l [i]
l [i]  = e
print l

```

File: ecrit_2006.tex, line 499

```
def moyenne (tab) :
s = 0.0
for x in tab :
s += x
return s / len (tab)

def variance (tab) :
s = 0.0
for x in tab :
t  = x - moyenne (tab)
s += t * t
return s / len (tab)

l = [ 0,1,2, 2,3,1,3,0]
print moyenne (l)
print variance (l)

```

File: ecrit_2006.tex, line 529

```
def variance (tab) :
s = 0.0
m = moyenne (tab)
for x in tab :
t  = x - m
s += t * t
return s / len (tab)

```

File: ecrit_2006.tex, line 563

```
class carre :
def __init__ (self, a) :
self.a = a
def surface (self) :
return self.a ** 2

class rectangle (carre) :
def __init__ (self, a,b) :
carre.__init__(self,a)
self.b = b
def surface (self) :
return self.a * self.b

```

File: ecrit_2006.tex, line 582

```
class rectangle :
def __init__ (self, a,b) :
self.a = a
self.b = b
def surface (self) :
return self.a * self.b

class carre (rectangle) :
def __init__ (self, a) :
rectangle.__init__ (self, a,a)
def surface (self) :
return self.a ** 2

```

File: ecrit_2006.tex, line 616

```
class carre :
def __init__ (self, a) :
self.a = a
def surface (self) :
return self.a ** 2

class rectangle (carre) :
def __init__ (self, a,b) :
carre.__init__(self,a)
self.b = b
def surface (self) :
return self.a * self.b

class losange (carre) :
def __init__ (self, a,theta) :
carre.__init__(self,a)
self.theta = theta
def surface (self) :
return self.a * math.cos (self.theta) * self.a * math.sin (self.theta) * 2

```

File: ecrit_2006.tex, line 654

```
x = 1.0
for i in range (0,15) :
x = x / 10
print i, "\t", 1.0 - x, "\t", x, "\t", x **(0.5)

```

File: ecrit_2006.tex, line 664

```
0    0.90000000000000002220     0.1              0.316227766017
1    0.98999999999999999112     0.01             0.1
2    0.99899999999999999911     0.001            0.0316227766017
3    0.99990000000000001101     0.0001           0.01
4    0.99999000000000004551     1e-05            0.00316227766017
5    0.99999899999999997124     1e-06            0.001
6    0.99999990000000005264     1e-07            0.000316227766017
7    0.99999998999999994975     1e-08            0.0001
8    0.99999999900000002828     1e-09            3.16227766017e-05
9    0.99999999989999999173     1e-10            1e-05
10   0.99999999998999999917     1e-11            3.16227766017e-06
11   0.99999999999900002212     1e-12            1e-06
12   0.99999999999989996891     1e-13            3.16227766017e-07
13   0.99999999999999000799     1e-14            1e-07
14   0.99999999999999900080     1e-15            3.16227766017e-08
15   0.99999999999999988898     1e-16            1e-08
16   1.00000000000000000000     1e-17            3.16227766017e-09
17   1.00000000000000000000     1e-18            1e-09
18   1.00000000000000000000     1e-19            3.16227766017e-10
19   1.00000000000000000000     1e-20            1e-10

```

File: ecrit_2006.tex, line 691

```
class matrice_carree_2 :
def __init__ (self, a,b,c,d) :
self.a, self.b, self.c, self.d = a,b,c,d

def determinant (self) :
return self.a * self.d - self.b * self.c

m1 = matrice_carree_2 (1.0,1e-6,1e-6,1.0)
m2 = matrice_carree_2 (1.0,1e-9,1e-9,1.0)
print m1.determinant ()
print m2.determinant ()

```

File: ecrit_2006.tex, line 729

```
def valeurs_propres (self) :
det   = self.determinant ()
trace = self.a + self.d
delta = trace ** 2 - 4 * det
l1    = 0.5 * (trace - (delta ** (0.5)) )
l2    = 0.5 * (trace + (delta ** (0.5)) )
return l1,l2

```

File: ecrit_2006.tex, line 768

```
0.99999999999900002212
1.00000000000000000000

```

File: ecrit_2006.tex, line 777

```
l1    = 0.5 * (trace - ((trace ** 2 - 4 * det) ** (0.5)) )
l2    = 0.5 * (trace + ((trace ** 2 - 4 * det) ** (0.5)) )

```

File: ecrit_2006.tex, line 785

```
l1 = 1,000001
l2 = 0.99999899999999997124  # égale à 1 - 1e-6

```

File: ecrit_2006.tex, line 793

```
l1 = 1
l2 = 1

```

File: ecrit_2006.tex, line 801

```
l1    = 0.5 * (trace - ((trace ** 2 - 4 * det) ** (0.5)) ) = - det ** 0.5 = -1e-9
l2    = 0.5 * (trace + ((trace ** 2 - 4 * det) ** (0.5)) ) = det ** 0.5 = 1e-9

```

File: ecrit_2006.tex, line 809

```
l1    = 1-1e-9  = 0.99999999900000002828
l2    = 1+ 1e-9 = 1.000000001

```