1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
| def plusgrandconsecutifs(L, n):
pmax = 0 # sauvegarde du produit maxi
ipmax = -1 # sauvegarde de son indice
for i in range(0, len(L)-n+1):
# calcul du produit
p = 1
for j in range(i, i+n):
p *= L[j]
# affichage de vérification (à supprimer après mise au point)
print i, L[i:i+n], p
# voir si le nouveau produit est plus grand que le précédent
if p>pmax:
# oui => on sauvegarde
pmax = p
ipmax = i
# on retourne le résultat
return ipmax, L[ipmax:ipmax+n], pmax
L = [20,3,5,1,60,4,9]
n = 3
i, L2, p = plusgrandconsecutifs(L, n)
print u"Résultat:", i, L2, p |