ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
def first_true(lo, hi, ok):
while lo < hi:
mid = (lo + hi) // 2
if ok(mid):
hi = mid
else:
lo = mid + 1
return lo
print(first_true(0, 100, lambda x: x * x >= 2024), first_true(5, 5, lambda x: True))- A. 44 5
- B. 45 6
- C. 45 5
- D. 46 5
Correct answer: C. 45 5
Binary search on a monotone predicate returns the first true point. 45 squared is 2025, the first square at or above 2024, and a range of one value returns its lower bound.
More Python Programming questions
- What is printed? python def search(a, t): lo, hi = 0, len(a) - 1 while lo <= hi: mid = (lo + hi) // 2 if a[mid] == t: return mid if a[lo]…
- What is printed? python import bisect a = sorted(['10', '9', '100', '20']) print(a, bisect.bisectleft(a, '9'), [int(x) for x in a])
- What is printed? python def arrows(p): p = sorted(p, key=lambda x: x[1]) cnt = 0 end = None for s, e in p: if end is None or s > end: cnt…
- What is printed? python from functools import cmptokey def largest(nums): s = [str(x) for x in nums] s.sort(key=cmptokey(lambda a, b: -1…
- What is printed? python from itertools import islice, count, takewhile sq = (i i for i in count(1)) print(list(islice(sq, 3))…
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def get(n): return [n] 2 a = get(3) a.append(99) b = get(3)…