05/02/2024 12:23 fct récursive.
py *
1 # version 3adiya
2 def SaisieR():
3 while 1:
4 try:n=int(input("entier"))
5 except: continue
6 return n
7 #version récurcive
8 def saisieR():
9 try: n=int(input("entier"))
10 except: return saisieR()
11 return n
12 #app de cour 1
13 def Moyserie(s,nb):
14 ent=saisieR()
15 if ent>0:
16 s+=ent
17 nb+=1
18 return Moyserie(s,nb)
19 else:
20 if nb !=0:return s/nb
21 '''print("{:.2f}".format(Moyserie(0,0)))'''
22 #app de cour 2
23 def Palindrome(ch):
24 if len(ch)>1:
25 if ch[0]!=ch[1]:return False
26 else: return Palindrome(ch[1:-1])
27 else : return True
28 #app de cour 3
29 def Rech(x,l):
30 if l!=[]:
31 if x==l[0]:return True
32 else:return Rech(x,l[1:])
33 else: return False
34 #ex1
35 def echcleval (dic,d):
36 if dic!={}:
37 t=[Link]()
38 d[t[1]]=t[0]
39 return echcleval(dic,d)
40 else: return d
41 #ex2
42 def Quotient(a,b):
43 if a>=b:
44 a-=b
45 return Quotient(a,b)+1
46 else : return 0
47 def Rest(a,b):
48 if a>=b:
49 a-=b
50 return Rest(a,b)
51 else :return a
52 #ex3
53 def Binaire (N):
54 if N!=0:
55 return Binaire(N//2)+str(n%2)
56 else: return""
57 #ex7 et 4
58 def longeur(ch):
59 if ch:
60 return longeur(ch[1:])+1
61 else: return 0
62 def Taille(l):
63 if l!=[]:
64 return [(l[0],longeur(l[0]))]+Taille(l[1:])
[Link] 1/3
05/02/2024 12:23 fct ré[Link] *
65 else:
66 return []
67 def compter(ch,car):
68 if ch!="":
69 if car==ch[0]:
70 return compter(ch[1:],car)+1
71 else:
72 return compter(ch[1:],car)
73 else :
74 return 0
75 def suite (n):
76 if n==0:return 0
77 elif n==1:return 1
78 else:return -3*suite(n-1)+2*suite(n-2)
79 # ex ecrire une fct récursive terminal saisieL qui remplie une liste L avec n entiers saisie >0
80 def saisieL (l=[],n):
81 if n!=0:
82 try:
83 x=int(input("entier>0"))
84 except: return saisieL(l,n)
85 if x>0:
86 [Link](x)
87 n-=1
88 return saisieL(l,n)
89 else: return l
90 #écrire une fct réc Affiche qui affiche les elem d'une liste L un à un
91 def Affiche1(l,i=0):
92 if i<len(l):
93 print(l[i])
94 Affiche1(l,i+1)
95 def affiche2(l):
96 if l:
97 print(l[0])
98 affiche2(l[1:])
99 #ecrire une fct réc SomL qui retourne la somme des entiers d'une liste L
100 #1
101 def SomL(l;i=0,s=0):
102 if i<len(l):
103 s+=l[i]
104 i+=1
105 return SomL(l,i,s)
106 else: return s
107 #2
108 def somL(l,s=0):
109 if l:
110 s+=l[0]
111 return somL(l[1:],s)
112 else: return s
113 #3
114 def Som_L(l):
115 if l:
116 return Som_L(l[1:])+l[0]
117 else : return 0
118 #ecrire une fct récursive liste9R qui retourne la liste vers 9 d'un entier n à deux chiffre
distincts
119 #1 terminal
120 def Liste9R(n,l):
121 if n!=9:
122 inv=n//10+(n%10)*10
123 [Link]([n,inv])
124 return Liste9R(abs(n-inv),l)
125 else : return L+[9]
126 #2 non terminal
127 def liste9R(n):
128 if n!=9:
[Link] 2/3
05/02/2024 12:23 fct ré[Link] *
129 inv=n//10 +(n%10)*10
130 return [n,inv]+liste9R(abs(n-inv))
131 '''ex à faire : ecrire une fct récursive syracuseR qui retounre dans une chaine les termes de
sycranuse séparer par , sauf le dernier à partir d'un entier n
132 si n=1 ==> le terme est 1
133 si n est paire ,on le divise par 2
134 si n est inpaire , on le multiple par 3 et on lui ajoute 1'''
135 '''terminal'''
136 def syracuseR(n,ch):
137 if n==1:
138 ch+="1"
139 return ch
140 else:
141 ch+=str(n)+","
142 if n%2==0:
143 n//=2
144 else: n=n*3+1
145 return syracuseR(n,ch)
146 '''non terminal'''
147 def SyracuseR1(n):
148 if n==1:
149 return "1"
150 elif n%2==0:
151 return str(n)+";"+SyracuseR1(n//2)
152 else:
153 return str(n)+";"+SyracuseR1(n*3+1)
154 '''ex anagrames: definir une fct anagR(ch1,ch2) retournant True si les 2 chaînes ch1 et ch2 sont
anagrammes (toutes les lettres de ch1 existent dans ch2avec le mème nbre d'occurances )et False sinon'''
155 def AnagR(ch1,ch2):
156 if ch1==ch2:
157 return True
158 else:
159 if ch1 and ch2:
160 nb1,nb2=[Link](ch1[0]),[Link](ch1[0])
161 if nb1==nb2: return AnagR([Link](ch1[0],"")),[Link](ch1[0],"")
162 return False
163 def anagR(ch1,ch2):
164 if ch1==ch2: return True
165 else:
166 if ch1:
167 ind=[Link](ch1[0])
168 if ind!=1:
169 return anagR(ch1[1:],ch2[:ind]+ch2[ind+1:])
170 return False
171 #Problème 1 série Recherche et récursivité
172 def SaisListRec(n,l):
173 if n==0: return
174 else:
175 try:
176 x=int(input("entier au moin à 2chiffres "))
177 except:return SaisListRec(n,l)
178 if x>=10:
179 [Link](x)
180 n-=1
181 return SaisListRec(n,l)
182
[Link] 3/3