ProgrammingModerateInfosys HackWithInfy · TCS CodeVita
What is printed?
What is printed?
def profit(p):
lo, best = float('inf'), 0
for x in p:
lo = min(lo, x)
best = max(best, x - lo)
return best
print(profit([7, 1, 5, 3, 6, 4]), profit([7, 6, 4, 3, 1]))- A. 4 0
- B. 5 1
- C. 0 5
- D. 5 0
Correct answer: D. 5 0
The code buys at the running minimum and sells at the current price. Buying at 1 and selling at 6 gives 5, while a falling sequence has no profitable trade and gives 0.
More Python Programming questions
- What is the time complexity of building a binary heap from n unordered items with heapq.heapify?
- Which statement about bisect.insort(a, x) on a plain Python list a is true?
- What is printed? python import itertools print(list(itertools.accumulate([3, 1, 4], initial=10)))
- What is printed? python from itertools import groupby s = 'aabbaa' print([(k, len(list(g))) for k, g in groupby(s)])
- What is printed? python print(sorted(['b', 'A', 'a', 'B'], key=str.lower))
- What is printed? python nums = map(int, '1 2 3'.split()) a = list(nums) b = list(nums) print(a, b)