Je n'arrive pas à comprendre pourquoi je reçois un message d'erreur lors de la compilation de la ligne en surbrillance ci-dessous. le message d'erreur est « ValueError: besoin de plus de 2 valeurs pour décompresser » ... Merci d'avance pour votre aideRecursive, Divide et Conquer Max Subarray
def divConHelper(array, first, center, last):
sumRight = float('-inf') #assign impossibly low number
sumLeft = float('-inf') #assign impossibly low number
leftIndexMax = 0
rightIndexMax = 0
# Determine crossover max toward left
currentLTotal = 0
for i in range(first, center):
#Incrementally add values to sum
currentLTotal += sum(array[i:center-1])
#if the total is greater than the sumLeft, replace
if currentLTotal > sumLeft:
sumLeft = currentLTotal
leftIndexMax = i
# Determine crossover max toward right
currentRTotal=0
for i in range(center+1, last):
#Incrementally add values to sum
currentRTotal += sum(array[center:i+1])
#if the rightTotal is greater than the sumRight, replace
if currentRTotal > sumRight:
sumRight = currentRTotal
rightIndexMax = i+1
bothTotal = sumRight + sumLeft
return(leftIndexMax, rightIndexMax, bothTotal)
#return(bothTotal)
def divConMaxSub(self, array, first, last):
if (last-first) == 0:
return array[first: last +1], array[0]
center = (first+last)// 2
#Recursive calls to divConMaxSub
(minA, maxA, sumA) = self.divConMaxSub(array, first, center)
(minB, maxB, sumB) = self.divConMaxSub(array, center+1, last)
#call to get max cross values
(maxL, maxR, maxCross) = divConHelper(array, first, center, last)
#Calulate the max subarray value
finalMax = max(sumA, sumB, maxCross)
#Logic block for return values
if (finalMax == sumA):
return array[minA: maxA], sumA
#return minA, maxA, sumA
elif (finalMax == sumB):
return array[minB: maxB], sumB
#return minB, maxB, sumB
elif (finalMax == maxCross):
return array[maxL: maxR], maxCross
#return maxL, maxR, maxCross
et voici mon code principal
from array import *
from utilities import utilities
from algorithms import algorithms
def main():
utils = utilities()
alg = algorithms()
results = utils.load()
for line in results:
max_sub_array, max_sum = alg.simpleenumeration(line)
utils.printtofile("Simple Enumeration", max_sub_array, max_sum)
max_sub_array, max_sum = alg.betterenumeration(line)
utils.printtofile("Better Enumeration", max_sub_array, max_sum)
max_sub_array, max_sum = alg.divConMaxSub(line)
utils.printtofile("Divide and Conquer", max_sub_array, max_sum)
max_sub_array, max_sum = alg.linear_sub_array(line)
utils.printtofile("Linear sub array", max_sub_array, max_sum)
# print("--------------------")
main()
Quelle ligne est en surbrillance? :) – Ashalynd
Hi- désolé, je suis nouveau à poser des questions ici :) ne peut pas sembler le mettre en évidence! Je fais référence aux appels récursifs à divConMaxSub: (minA, maxA, sumA) = self.divConMaxSub (tableau, premier, centre) (minB, maxB, sumB) = self.divConMaxSub (array, center + 1, dernier) – tls11