Vous êtes sur la page 1sur 2

Exercices-cours-récursivité

Exercice 1 : Exercice 5 :

def décroissant(n): def somseq(s):

if n==0: if len(s)==0:

print('') return 0

else: else:

print(n) return int(s[0])+somseq(s[1:])

décroissant(n-1) Exercice 6 :
Exercice 2 : def rev(L):

def croissant(n): if L==[]:

if n==0: return L

print('') else:

else: return [L[-1]]+rev(L[:-1])

croissant(n-1) Exercice 7 :

print(n) def palindrome(ch):

Exercice 3 : if len(ch)<2:

def somme(a): return True

if a==1: if ch[0]!=ch[-1]:

return 1 return False

else: return palindrome(ch[1:-1])

return a*a+somme(a-1) Exercice 8 :


def len_recur(L):
Exercice 4 : def len_iter(L):
if L==[]:
c=0
def som(a):
for I in L: return 0
if a<10:
c=c+1 else:
return a
return c return 1+ len_recur(L[1:])
else:

return a%10 +som(n//10)

1
Version itérative :
Exercice 9 :
def reverse(L):
def reverse(L):
seq=[]
if len(L)==0:
for i in range(len(L)):
return []
seq.insert(0,L[i])
else:
return seq
return reverse(L[1:])+[L[0]]

Exercice 10 :
def max_iterative(L):
m=L[0] def max_recursive(L):
for i in L:
if len(L)==1:
if i>m:
m=i return L[0]
return m
elif L[0]>L[1]:
while True:
try: L[1]=L[0]
n= int(input("donner la taille :"))
L=L[1:]
if n==0:
raise ValueError('la taille de la liste doit être non nulle') return max_recursive(L)
break
except ValueError:
print("Essayez de nouveau")
L=[]
for i in range(n):
L.append(float(input('donner un élément')))
print("le maximum est",max_iterative(L) )

Vous aimerez peut-être aussi