Vous êtes sur la page 1sur 4

Correction TD-fonc-rec

Exercice 1 :
def verif(T):
if len(T)<2:
return True
elif T[0]<T[1]:
return False
else:
return verif (T[1:])

Exercice 2 :
def chiffre_itérative (ch) :
L=[]
for i in range(len(ch)) :
x=int(ch[i])
L.append(x)
return L

def chiffre_récursive(ch):
if ch=="":
return []
else:
return [int(ch[0])]+chiffre_récursive(ch[1:])

Exercice 3 :
def diviseur(a,b):

if b==a:

return True

elif b<a:

return False

else:

return diviseur(a,b-a)

1
Exercice 4 :
def doublons (n) :

ch=str(n)

for k in range(len(ch)-1):

if ch[k] in ch[k+1:] :

return True

return False

Exercice 5 :
1-

def f (x,y) :

if x>y:

return x**2-7*x*y+1

elif x<y:

return y**3+4*x-13

else:

return y+1

2-

while True:

try:

x = float(input("Veuillez saisir un nombre : "))

y = float(input("Veuillez saisir un nombre : "))

break

except ValueError:

print("Vous n’avez pas saisi deux réels. Essayez de nouveau...")

val=f(x,y)

print("La valeur de la fonction f pour x=",x, "et", " y=", y, "est", val)

3-

2
Fonction itérative Fonction récursive

def Suite_iter(n) : def Suite_recur(n) :

u=0 i f n == 0 :
return 0
for i in range(n) :
else :
u = f(0,u)
return f(0, Suite_recur(n-1))
return u

4-

def Premier (a) :

u,i = 0,0

while u < a :

u = f(0,u)

i += 1

return i

5-

def Liste (n):

L=[]

for i in range(n+1):

L. append(Suite_iter(i))

return L

6-

def Test(L) :

for j in range(len(L)):

if L[j] % 2 != 0:

return j

return -1

7-

def Nom (b,L):

n=0

3
for k in L:

if b==k:

n=n+1

return n

8- Cette fonction sert à la recherche d’un élément majoritaire (ayant le plus grand nombre
d’apparitions) dans une liste L. Sa complexité est O(n2).

9- Existe = lambda b, L : b in L

10-

def Commun (L1,L2):

L=[]

for i in range(len(L1)):

if Existe(L1[i],L2):

L.append(L1[i])

return L

11-

while True:

try:

x = float(input("Veuillez saisir un nombre : "))

y = float(input("Veuillez saisir un nombre : "))

break

except ValueError:

print("Vous n’avez pas saisi deux réels. Essayez de nouveau...")

val=f(x,y)

n = int(input("Veuillez saisir un entier : "))

L=Liste(n)

dict={}

for m in L :

dict[m]= Nom(m,L)

print(dict)

Vous aimerez peut-être aussi