I am currently taking 6.00.2x from MITx, and there is a line from a search tree algorithm that confuses me, could anyone help please?
val, taken = maxVal(foods, maxUnits)
This syntax does not make sense to me.
maxVal is a function, so presumably
maxUnits are inputs. But what are
taken, what does this line do? Nowhere in the code are there variables instantiated with those names, so I am just not sure what they are (and this line of syntax means).
PS: The complete code is as follows. The aforementioned syntax occurs on 3rd line of the function
foods is a list of 1) food, 2) values, and 3) calories.
def maxVal(toConsider, avail): """Assumes toConsider a list of items, avail a weight Returns a tuple of the total value of a solution to the 0/1 knapsack problem and the items of that solution""" if toConsider ==  or avail == 0: result = (0, ()) elif toConsider.getCost() > avail: #Explore right branch only result = maxVal(toConsider[1:], avail) else: nextItem = toConsider #Explore left branch withVal, withToTake = maxVal(toConsider[1:], avail - nextItem.getCost()) withVal += nextItem.getValue() #Explore right branch withoutVal, withoutToTake = maxVal(toConsider[1:], avail) #Choose better branch if withVal > withoutVal: result = (withVal, withToTake + (nextItem,)) else: result = (withoutVal, withoutToTake) return result def testMaxVal(foods, maxUnits, printItems = True): print('Use search tree to allocate', maxUnits, 'calories') val, taken = maxVal(foods, maxUnits) print('Total value of items taken =', val) if printItems: for item in taken: print(' ', item) testMaxVal(foods, 750)