Re: Rekursive Funktion
Verfasst: Dienstag 29. Mai 2018, 09:44
mhm, klingt natürlich logisch. wie ich das in meinen code implementiere, ist mir nicht klar
Seit 2002 Diskussionen rund um die Programmiersprache Python
https://www.python-forum.de/
Code: Alles auswählen
def maximum(a,p): #Liste a mit Länge p (len(a)=p)
if p==1 : #ist nur ein Element in der Liste
return a[0]
else:
maxi = max(a[1:p+1]) #Suche maximum von a[1] bis a[p]
if a[0] > maxi : #prüfe ob gefundenes Element a[maxi] > a[0] ist
return a[0]
else:
return maxi
Code: Alles auswählen
def maximum(a,p):
return max(a[:p+1])
Code: Alles auswählen
a = [5,6,7,2,3,2,5,3,7,3,2,7,8]
def maximum(a,p):
if p<0:
raise ValueError("cannot find maximum of an empty sequence")
elif p==0:
return a[p]
else:
if maximum(a,p-1)<a[p]:
return a[p]
else:
return maximum(a,p-1)
print(maximum(a,1))
Code: Alles auswählen
if maximum(a,p-1)<a[p]:
return a[p]
else:
return maximum(a,p-1)
Code: Alles auswählen
a = [5,6,7,2,3,2,5,3,7,3,2,7,8]
def maximum(a,p):
if p<0:
raise ValueError("cannot find maximum of an empty sequence")
elif p==0:
return a[p]
else:
b = maximum(a,p-1)
if maximum(a,p-1)<a[p]:
return a[p]
else:
return b
print(maximum(a,6))
Code: Alles auswählen
a = [5,6,7,2,3,2,5,3,7,3,2,7,8]
def maximum(a,p):
if p<0:
raise ValueError("cannot find maximum of an empty sequence")
elif p==0:
return a[p]
else:
b = maximum(a,p-1)
c = a[p]
if b<c:
return c
else:
return b
print(maximum(a,6))