Initiation à la programmation ENSAE 1A

ecrit_2009.tex

File: ecrit_2009.tex, line 45


def f():
    x = 0
    print x


f()

def g():
    print x

g()

File: ecrit_2009.tex, line 61


def f():
    x = 0
    print x

x = 1
f()

def g():
    print x

g()

File: ecrit_2009.tex, line 89


Traceback (most recent call last):
  File "p1.py", line 11, in <module>
    g()
  File "p1.py", line 9, in g
    print x
NameError: global name 'x' is not defined

File: ecrit_2009.tex, line 106



a       = [ None, 0 ]
b       = [ None, 1 ]
a [0]   = b
b [0]   = a
print a [0][0] [0][0] [0][0] [1]

File: ecrit_2009.tex, line 118


import copy
a       = [ None, 0 ]
b       = [ None, 1 ]
a [0]   = copy.copy (b)
b [0]   = a
print a [0][0] [0][0] [0][0] [1]

File: ecrit_2009.tex, line 141


0
1

File: ecrit_2009.tex, line 147


a       = [ None, 0 ]
b       = [ None, 1 ]
a [0]   = b   # b et a [0] désignent la même liste
b [0]   = a   # b [0] et a désignent la même liste

File: ecrit_2009.tex, line 171


0

File: ecrit_2009.tex, line 204


import random
def somme_alea (tableau, n) :
    s = 0
    for i in range (0, n) :
        h = random.randint (0, len (tableau)) 
        s += tableau [h]
    print s / n
x = somme_alea ( range (0,100), 10)
print "x = ", x

File: ecrit_2009.tex, line 223


44
x =  None

File: ecrit_2009.tex, line 234


Traceback (most recent call last):
  File "examen2009.py", line 10, in <module>
    print somme_alea ( range (0,100), 10)
  File "examen2009.py", line 8, in somme_alea
    s += tableau [h]
IndexError: list index out of range

File: ecrit_2009.tex, line 253


44

File: ecrit_2009.tex, line 258


h = random.randint (0, len (tableau)-1)

File: ecrit_2009.tex, line 288


def moyenne_mobile (suite, m) :
    res = []
    for i in range (m, len (suite)-m) :
        s = 0
        for j in range (-m,m+1) : s += suite [i+j]
        res.append (  float (s) / (m*2+1) )
    return res

File: ecrit_2009.tex, line 301


def moyenne_mobile (suite, m) :
    res = []
    for i in range (m, len (suite)-m) :
        if i == m :
            # ... ligne à remplacer    
        else :
            # ... ligne à remplacer    
        res.append (  float (s) / (m*2+1) )
    return res

File: ecrit_2009.tex, line 333


def moyenne_mobile (suite, m) :
    res = []
    for i in range (m, len (suite)-m) :
        if i == m :
            s = sum ( suite [i-m : i+m+1] )
        else :
            s = s - suite [i-m-1] + suite [i+m]
        res.append (  float (s) / (m*2+1) )
    return res

File: ecrit_2009.tex, line 358


def fonction_mystere (tableau) :
    for i in range (0, len (tableau)) :
        if tableau [i] % 2 != 0 : continue
        pos = i
        for j in range (i+1, len (tableau)) :
            if tableau [j] % 2 != 0 : continue
            if tableau [pos] < tableau [j] : pos = j
        ech           = tableau [i]
        tableau [i]   = tableau [pos]
        tableau [pos] = ech

File: ecrit_2009.tex, line 375


if tableau [i] % 2 != 0 : continue
    if tableau [pos] < tableau [j] : pos = j

File: ecrit_2009.tex, line 381


def fonction_mystere (tableau) :
    for i in range (0, len (tableau)) :
        pos = i
        for j in range (i+1, len (tableau)) :
            if tableau [pos] < tableau [j] : pos = j
        ech           = tableau [i]
        tableau [i]   = tableau [pos]
        tableau [pos] = ech

File: ecrit_2009.tex, line 393


a = [ 0, 1, 4, 3, 5, 2 ]
print a                     # affiche [ 0, 1, 4, 3, 5, 2 ]
b = fonction_mystere (a)
print b                     # affiche [ 4, 1, 2, 3, 5, 0 ]

File: ecrit_2009.tex, line 494


class Boucle :
    def __init__ (self, a, b) :
        self.a = a
        self.b = b
    def iteration (self, i) :              # contenu d'une itération
        pass                               
    def boucle (self) :                    # opère la boucle
        for i in range (self.a, self.b) : 
            self.iteration (i)

File: ecrit_2009.tex, line 510


class Gauss (Boucle) :
    def __init__ (self, b) :
        Boucle.__init__ (self, 1, b+1)
        self.x = 0
        
    def iteration (self,i) :
        self.x += i
        
g = Gauss (10)
g.boucle ()
print g.x

File: ecrit_2009.tex, line 542


class Carre (Gauss) :
    def iteration (self,i) :
        self.x += i*i           # ligne changée
        
g = Carre (10)
g.boucle ()
print g.x

File: ecrit_2009.tex, line 554


class Boucle2 (Boucle) :
    def __init__ (self, a, b, c, d) :
        Boucle.__init__ (self, a, b)    
        self.c = c
        self.d = d
    def iteration (self, i, j) :
        pass
    def boucle (self) :
        for j in range (self.c, self.d) : 
            for i in range (self.a, self.b) : 
                self.iteration (i,j)
                
class Gauss2 (Boucle2) :
    def __init__ (self, a, b) :
        Boucle2.__init__ (self, 1, a+1, 1, b+1)
        self.x = 0
    def iteration (self, i,j) :
        self.x += i+j                

File: ecrit_2009.tex, line 577


class Boucle2 (Boucle) :
    def __init__ (self, a, b, c, d) :
        Boucle.__init__ (self, a, b)    
        self.c = c
        self.d = d
    def iteration (self, i) :
        pass
    def boucle (self) :
        for j in range (self.c, self.d) : 
            self.j = j
            for i in range (self.a, self.b) : 
                self.iteration (i)
                
class Gauss2 (Boucle2) :
    def __init__ (self, a, b) :
        Boucle2.__init__ (self, 1, a+1, 1, b+1)
        self.x = 0
    def iteration (self, i) :
        self.x += self.j+i

File: ecrit_2009.tex, line 601


class Boucle2 (Boucle) :
    def __init__ (self, a, b, c, d) :
        Boucle.__init__ (self, a, b)    
        self.c = c
        self.d = d
    def iteration (self, i) :
        pass
    def boucle (self) :
        for j in range (self.c, self.d) : 
            self.j = j
            Boucle.boucle (self)  # suppression d'une boucle
                                  # caché dans la classe Boucle

File: ecrit_2009.tex, line 618


class Boucle2 (Boucle) :
    def __init__ (self, a, b, c, d) :
        Boucle.__init__ (self, a, b)    
        self.c = c
        self.d = d
        
    def iteration (self, i) :
        for j in range (self.c, self.d) :
            self.iteration2 (i,j)
            
    def iteration2 (self, i,j) :
        pass
        
                        
class Gauss2 (Boucle2) :
    def __init__ (self, a, b) :
        Boucle2.__init__ (self, 1, a+1, 1, b+1)
        self.x = 0
    def iteration2 (self, i, j) :
        self.x += j+i

File: ecrit_2009.tex, line 644


class Boucle2 (Boucle) :
    def __init__ (self, a, b, c, d) :
        Boucle.__init__ (self, a, b)    
        self.c = c
        self.d = d
    def iteration (self, i, j) :
        pass
    def boucle (self) :
        ab = self.b - self.a
        cd = self.d - self.c
        n  = ab * cd
        for k in range (0, n) :
            i = k % ab + self.a
            j = k / ab + self.c
            self.iteration (i,j)