Embercode
✕
Dynamic ProgrammingQuick quiz

Unbounded knapsack

6 questions. Covers Unbounded knapsack and rod cutting in Python.

Questions 1 to 5 of 6

  1. 1. What number does this program print? ``python weights = [3] values = [4] capacity = 7 best = [0] * (capacity + 1) for w in range(1, capacity + 1): for i in range(len(weights)): if weights[i] <= w: best[w] = max(best[w], best[w - weights[i]] + values[i]) print(best[capacity]) ``
  2. 2. What is the last line printed by this program? ``python weights = [2] values = [5] capacity = 5 best = [0] * (capacity + 1) for w in range(capacity, 0, -1): for i in range(len(weights)): if weights[i] <= w: best[w] = max(best[w], best[w - weights[i]] + values[i]) print(best) print(best[capacity]) ``
  3. 3. Why does the unbounded knapsack loop let the same item be taken more than once?
  4. 4. The rod cutting prices from the lesson are: length 1 sells for 1, length 2 for 5, length 3 for 8, length 4 for 9, and length 5 for 10. What is the best total for a rod of length 5?
  5. 5. Using the lesson's rod prices (length 1 for 1, 2 for 5, 3 for 8, 4 for 9, 5 for 10, 6 for 17, 7 for 17, 8 for 20), what is the best total for a rod of length 8?

    Type your answer.

5 question(s) left on this page.

Typed answers ignore extra spaces and, for most questions, capital letters.