ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
def content(g, s):
g.sort(); s.sort()
i = j = 0
while i < len(g) and j < len(s):
if s[j] >= g[i]:
i += 1
j += 1
return i
print(content([1, 2, 3], [1, 1]), content([10, 9, 8, 7], [5, 6, 7, 8]))- A. 2 2
- B. 1 2
- C. 1 1
- D. 0 2
Correct answer: B. 1 2
Sorting both lists and giving each child the smallest cookie that satisfies them is the correct greedy. Only one child is satisfied in the first case and two in the second.
More Python Programming questions
- What is printed? python from collections import Counter def least(tasks, n): c = Counter(tasks) m = max(c.values()) k = sum(1 for v in…
- What is printed? python s = 'abcdefg' print(s[::-2], s[5:1:-2], s[-3::-3])
- What is printed? python a = {'x': 1, 'y': 2} b = {'y': 3, 'z': 4} c = a | b a.update(b, x=9) print(c, a)
- What is printed? python import heapq h = [5, 7, 9] heapq.heapify(h) r1 = heapq.heappushpop(h, 6) r2 = heapq.heappushpop(h, 1) print(r1…
- What is printed? python import bisect a = [1, 3, 3, 5, 7] bisect.insort(a, 3) bisect.insortright(a, 4, lo=0, hi=3) print(a…
- What is printed? python def f(x): out = [] try: out.append('t') if x: raise ValueError except ValueError: out.append('e') else…