i can't seem figure out why i'm getting error message on compile highlighted line below. error message "valueerror: need more 2 values unpack"... thank in advance
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 in range(first, center): #incrementally add values sum currentltotal += sum(array[i:center-1]) #if total greater sumleft, replace if currentltotal > sumleft: sumleft = currentltotal leftindexmax = # determine crossover max toward right currentrtotal=0 in range(center+1, last): #incrementally add values sum currentrtotal += sum(array[center:i+1]) #if righttotal greater 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 divconmaxsub (mina, maxa, suma) = self.divconmaxsub(array, first, center) (minb, maxb, sumb) = self.divconmaxsub(array, center+1, last) #call max cross values (maxl, maxr, maxcross) = divconhelper(array, first, center, last) #calulate max subarray value finalmax = max(suma, sumb, maxcross) #logic block 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
and here main code
array import * utilities import utilities algorithms import algorithms def main(): utils = utilities() alg = algorithms() results = utils.load() 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 , 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()
this statement:
return array[mina: maxa], suma
does not return 3 arguments (as seem expect in code):
(mina, maxa, suma) = self.divconmaxsub(array, first, center)
it returns chunk of array first argument , sum second. therefore, python complains cannot use output of divconmaxsub initialize 3 values on left, because 2 values available on right.
Comments
Post a Comment