#### [SOLVED] Confusing syntax from a search tree algorithm

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 `foods` and `maxUnits` are inputs. But what are `val` and `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 `testMaxVal`. `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[0].getCost() > avail:
#Explore right branch only
result = maxVal(toConsider[1:], avail)
else:
nextItem = toConsider[0]
#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)
``````

#### @molbdnilo 2019-09-11 09:24:14

`maxVal` returns a pair.
You can "deconstruct" any tuple by assigning its elements to the appropriate number of variables simultaneously.

Example:

``````>>> a,b,c = (1,2, "hello")
>>> a
1
>>> b
2
>>> c
'hello'
``````

#### @King's jester 2019-09-11 09:24:02

As you can see, maxVal can return two outputs at the same time like at the line :

``````result = (withoutVal, withoutToTake)
``````

Recover these two outputs in two variable val and taken is done by the line :

``````val, taken = maxVal(foods, maxUnits)
``````

#### @Kshitij Saxena 2019-09-11 09:23:54

The function `maxVal` returns a `tuple`. You can return multiple values from a function in python in the form of `tuple`.

Example:

``````def connect():
connection = _connect()
message = "Connected"
if not connection:
message = "Not connected"
return connection, message

connection, message = connect()
``````

### [SOLVED] How to remove an element from a list by index

• 2009-03-09 18:16:11
• Joan Venge
• 2458842 View
• 1413 Score
• Tags:   python list

### [SOLVED] Delete an element from a dictionary

• 2011-04-30 21:20:57
• richzilla
• 1487356 View
• 1278 Score
• Tags:   python dictionary del

### [SOLVED] How to randomly select an item from a list?

• 2008-11-20 18:42:21
• Ray Vega
• 1336088 View
• 1688 Score
• Tags:   python list random

### [SOLVED] How do you read from stdin?

• 2009-09-20 05:48:07
• tehryan
• 1527551 View
• 1408 Score
• Tags:   python stdin