ProgrammingHardCAT · MBA placement
What is printed?
What is printed?
def units(boxes, cap):
boxes = sorted(boxes, key=lambda b: -b[1])
total = 0
for n, u in boxes:
take = min(n, cap)
total += take * u
cap -= take
if cap == 0:
break
return total
print(units([[1, 3], [2, 2], [3, 1]], 4), units([[5, 10], [2, 5]], 3))- A. 8 30
- B. 7 30
- C. 8 25
- D. 9 30
Correct answer: A. 8 30
Boxes with more units each are loaded first, and the truck takes as many boxes as it can from each type. The first case gives 3 + 2 x 2 + 1 x 1 = 8, and the second gives 30.
More Python Programming questions
- What is printed? python def minremove(iv): iv = sorted(iv, key=lambda x: x[1]) keep = 0 end = float('-inf') for s, e in iv: if s >= end…
- What is printed? python def longest(s): st = [-1] best = 0 for i, ch in enumerate(s): if ch == '(': st.append(i) else: st.pop() if not st…
- What is printed? python import heapq from collections import Counter words = 'a b c a b a d d d d'.split() c = Counter(words)…
- What is printed? python from functools import cache @cache def cat(n): if n <= 1: return 1 return sum(cat(i) cat(n - 1 - i) for i in…
- What is printed? python from functools import lrucache grid = [[1, 3, 1], [1, 5, 1], [4, 2, 1]] @lrucache(maxsize=None) def best(r, c): if…
- What is printed? python def sigma(n): return sum(d for d in range(1, n + 1) if n % d == 0) perfect = [n for n in range(2, 30) if sigma(n)…