ProgrammingVery hardCAT · MBA placement

What is printed?

What is printed?
def cool(p):
    hold, sold, rest = float('-inf'), 0, 0
    for x in p:
        hold, sold, rest = max(hold, rest - x), hold + x, max(rest, sold)
    return max(sold, rest)
print(cool([1, 2, 3, 0, 2]))
  1. A. 2
  2. B. 5
  3. C. 4
  4. D. 3

Correct answer: D. 3

Three states track holding, just sold and resting, and a sale must be followed by a rest day. The best plan buys at 1, sells at 2, rests, buys at 0 and sells at 2, for a total profit of 3.

More Python Programming questions

Exams that ask this topic