By Daniel Mak

2019-09-11 09:19:19 8 Comments

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)
        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,))
            result = (withoutVal, withoutToTake)
    return result

def testMaxVal(foods, maxUnits, printItems = True):
    print('Use search tree to allocate', maxUnits,
    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.


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

@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.


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

connection, message = connect()

Related Questions

Sponsored Content

10 Answered Questions

[SOLVED] How to remove a key from a Python dictionary?

13 Answered Questions

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

  • 2008-11-20 18:42:21
  • Ray Vega
  • 1277850 View
  • 1613 Score
  • 13 Answer
  • Tags:   python list random

22 Answered Questions

[SOLVED] Importing files from different folder

19 Answered Questions

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

  • 2009-03-09 18:16:11
  • Joan Venge
  • 2299611 View
  • 1302 Score
  • 19 Answer
  • Tags:   python list

21 Answered Questions

[SOLVED] How do you read from stdin?

  • 2009-09-20 05:48:07
  • tehryan
  • 1432340 View
  • 1352 Score
  • 21 Answer
  • Tags:   python stdin

15 Answered Questions

[SOLVED] Delete an element from a dictionary

  • 2011-04-30 21:20:57
  • richzilla
  • 1389389 View
  • 1190 Score
  • 15 Answer
  • Tags:   python dictionary del

10 Answered Questions

[SOLVED] Select rows from a DataFrame based on values in a column in pandas

10 Answered Questions

21 Answered Questions

[SOLVED] Directory-tree listing in Python

20 Answered Questions

[SOLVED] Extracting extension from filename in Python

Sponsored Content