# Initiation à la programmation ENSAE 1A

## ecrit_2008.tex

File: ecrit_2008.tex, line 31

```
def arrondi_05 (x) :
return float (int (x * 2 + 0.5)) / 2

def arrondi_0125 (x) :
return float (int (x * 8 + 0.5)) / 8

def arrondi (x, p) :
return float (int (x / p + 0.5)) * p

```

File: ecrit_2008.tex, line 51

```
for a in range (0, 10) :
for b in range (0, 10) :
for c in range (0, 10) :
print [a,b,c]

```

File: ecrit_2008.tex, line 69

```
a,b,c = 0,0,0
while c < 10 :
print [a,b,c]
a += 1
if a == 10 :
b += 1
a = 0
if b == 10 :
c += 1
b = 1

```

File: ecrit_2008.tex, line 86

```
l = [0,0,0]
while l [-1] < 10 :
print l
l [0] += 1
i = 0
while i < len (l)-1 and l [i] == 10 :
l [i] = 0
l [i+1] += 1
i += 1

```

File: ecrit_2008.tex, line 112

```
def fibo (n) :
if n <= 2 : return 2
else : return fibo (n-1) + fibo (n-2)

```

File: ecrit_2008.tex, line 137

```
nb = 0  # variable globale
def fibo (n,p) :
global nb
if n <= 2 :
nb += 1
return p # plus de récurrence
else :
nb += 2
return fibo (n-1,p) + fibo (n-2,p)

for n in range (1, 20) :
nb = 0    # remis à zéro, à chaque fois
# nb est la mesure du coût
print fibo(n,3)-2, nb  # nombres identiques
# nb vérifie la récurrence de la suite c(n)
#          c(n) = c(n-1) + c(n-2) + 2

```

File: ecrit_2008.tex, line 209

```
l = [0,1,2,3,4,5]
g = l
for i in range (0, len (l)-1) :
g [i] = g [i+1]
print l
print g

```

File: ecrit_2008.tex, line 221

```
l = [0,1,2,3,4,5]
g = [0,1,2,3,4,5]
for i in range (0, len (l)-1) :
g [i] = g [i+1]
print l
print g

```

File: ecrit_2008.tex, line 233

```
l = [0,1,2,3,4,5]
g = [0,1,2,3,4,5]
for i in range (0, len (l)) :
g [i] = g [(i+1)%len (l)]
print l
print g

```

File: ecrit_2008.tex, line 253

```
[1, 2, 3, 4, 5, 5]
[1, 2, 3, 4, 5, 5]

```

File: ecrit_2008.tex, line 261

```
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 5]

```

File: ecrit_2008.tex, line 269

```
[0, 1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 1]

```

File: ecrit_2008.tex, line 277

```
l = [0,1,2,3,4,5]
g = [0,1,2,3,4,5]
for i in range (0, len (l)) :
g [i] = l [(i+1)%len (l)] # ligne modifiée, g devient l
print l
print g

```

File: ecrit_2008.tex, line 291

```
import copy
l = [0,1,2,3,4,5]
g = copy.copy (l) # on pourrait aussi écrire g = list (l)
# ou encore g = [ i for i in l ]
for i in range (0, len (l)) :
g [i] = l [(i+1)%len (l)]
print l
print g

```

File: ecrit_2008.tex, line 316

```
def mystere (l) :
"""cette fonction s'applique à des listes de nombres"""
l.sort ()
nb = 0
for i in range (1,len (l)) :
if l [i-1] != l [i] : nb += 1
return nb+1

l = [4,3,1,2,3,4]
print mystere (l)  # affiche 4

```

File: ecrit_2008.tex, line 342

```
[1, 2, 3, 3, 4, 4]

```

File: ecrit_2008.tex, line 350

```
import copy
def mystere (l) :
"""cette fonction s'applique à des listes de nombres"""
l = copy.copy (l) # ligne insérée
# on peut écrire aussi l = list (l)
l.sort ()
nb = 0
for i in range (1,len (l)) :
if l [i-1] != l [i] : nb += 1
return nb+1

```

File: ecrit_2008.tex, line 376

```
class Personne :
def __init__ (self, nom) :
self.nom = nom
def entete (self) :
return ""
def __str__ (self) :
s = self.entete () + self.nom
return s

class Homme (Personne) :
def __init__ (self, nom) :
Personne.__init__ (self, nom)
def entete (self) :
return "M. "

class Femme (Personne) :
def __init__ (self, nom) :
Personne.__init__ (self, nom)
def entete (self) :
return "Melle "

h = Homme ("Hector")
f = Femme ("Gertrude")
print h
print f

```

File: ecrit_2008.tex, line 406

```
class Personne :
def __init__ (self, nom, entete) :
self.nom = nom
self.entete = entete
def __str__ (self) :
s = self.entete + self.nom
return s

h = Personne ("Hector", "M. ")
f = Personne ("Gertrude", "Melle ")
print h
print f

```

File: ecrit_2008.tex, line 450

```
M. Hector
Melle Gertrude

```

File: ecrit_2008.tex, line 459

```
class Personne :
def __init__ (self, nom) :
self.nom = nom
def entete (self) :
return ""
def __str__ (self) :
s = self.entete () + self.nom
return s

class Homme (Personne) :
def __init__ (self, nom) :
Personne.__init__ (self, nom)
def entete (self) :
return "M. "

class Femme (Personne) :
def __init__ (self, nom) :
Personne.__init__ (self, nom)
def entete (self) :
return "Melle "

class Hermaphrodite (Personne) :
def __init__ (self, nom) :
Personne.__init__ (self, nom)
def entete (self) :
return "Melle et M. "

h = Homme ("Hector")
f = Femme ("Gertrude")
g = Hermaphrodite ("Marie-Jean")
print h
print f
print g

```

File: ecrit_2008.tex, line 497

```
class Personne :
def __init__ (self, nom, entete) :
self.nom = nom
self.entete = entete
def __str__ (self) :
s = self.entete + self.nom
return s

h = Personne ("Hector", "M. ")
f = Personne ("Gertrude", "Melle ")
g = Personne ("Marie-Jean", \
"Melle et M. ")
print h
print f
print g

```

File: ecrit_2008.tex, line 552

```
def tri_entiers(l):
"""cette fonction s'applique à une liste d'entiers"""

# groupe 1
m = l [0]
M = l [0]
for k in range(1,len(l)):
if l [k] < m : m = l [k]
if l [k] > M : M = l [k]

# groupe 2
p = [0 for i in range (m,M+1) ]
for i in range (0, len (l)) :
p [ l [i] - m ] += 1

# groupe 3
R     = [0 for i in range (m,M+1) ]
R [0] = p [0]
for k in range (1, len (p)) :
R [k] = R [k-1] + p [k]

# groupe 4
pos = 0
for i in range (1, len (l)) :
while R [pos] < i : pos += 1
l [i-1] = pos + m
l [len (l)-1] = M

```

File: ecrit_2008.tex, line 643

```
import random

def tirage (poids) :
nb = [ 0 for p in poids ]
while True :
i = random.randint (0, len (poids)-1)
nb [i] += 1
if nb [i] == poids [i] :
return i

salaire = [ 10000, 5000, 3000, 2000 ]
poids   = [ int (s / 1000) for s in salaire ]
nombre  = [ 0 for s in salaire ]

for n in range (0,1000) :
p = tirage (poids)
nombre [p] += 1

for i in range (0, len (poids)) :
print "salaire ", salaire [i], " : nb : ", nombre [i]

```

File: ecrit_2008.tex, line 669

```
salaire  10000  : nb :  0
salaire  5000  : nb :  49
salaire  3000  : nb :  301
salaire  2000  : nb :  650

```

File: ecrit_2008.tex, line 685

```
import random

def tirage (poids, nb) :
while True :
i = random.randint (0, len (poids)-1)
nb [i] += 1
if nb [i] % poids [i]  == 0 :
return i

salaire = [ 10000, 5000, 3000, 2000 ]
poids   = [ int (s / 1000) for s in salaire ]
nombre  = [ 0 for s in salaire ]
temp    = [ 0 for s in salaire ]

for n in range (0,1000) :
p = tirage (poids, temp)
nombre [p] += 1

for i in range (0, len (poids)) :
print "salaire ", salaire [i], " : nb : ", nombre [i]

```

File: ecrit_2008.tex, line 711

```
salaire  10000  : nb :  90
salaire  5000  : nb :  178
salaire  3000  : nb :  303
salaire  2000  : nb :  429

```

File: ecrit_2008.tex, line 747

```
dico = { }
dico ["Jean"] = { }
dico ["Jean"] ["Matthieu"] = { }
dico ["Jean"] ["Pierre"] = { }
dico ["Jean"] ["Matthieu"] ["Thomas"] = { }
dico ["Jean"] ["Matthieu"] ["Louis"] = { }
dico ["Jean"] ["Pierre"] ["Anne"] = dico ["Jean"] ["Matthieu"] ["Thomas"]
dico ["Jean"] ["Pierre"] ["Henri"] = { }
dico ["Jean"] ["Pierre"] ["Anne"] ["Alphonse"] = { }

def print_dico (dico, niveau = 0) :
decalage = "...." * niveau
for d in dico :
print decalage, d
if len (dico [d]) > 0 :
print_dico (dico [d], niveau+1)

print_dico (dico)  # première fois
dico ["Jean"] ["Pierre"] ["Anne"] ["Bernard"] = { }  ### ligne B
print "**********"
print_dico (dico)  # seconde fois

```

File: ecrit_2008.tex, line 774

```
Jean
.... Matthieu
........ Louis
........ Thomas
............ Alphonse
.... Pierre
........ Anne
............ Alphonse
........ Henri
**********
Jean
.... Matthieu
........ Louis
........ Thomas
............ Bernard
............ Alphonse
.... Pierre
........ Anne
............ Bernard
............ Alphonse
........ Henri

```