ProgrammingHardTCS NQT · Accenture
What is printed?
What is printed?
def dedupe(a):
if not a:
return 0
k = 1
for i in range(1, len(a)):
if a[i] != a[k - 1]:
a[k] = a[i]
k += 1
return k
arr = [0, 0, 1, 1, 1, 2, 2, 3, 3, 4]
n = dedupe(arr)
print(n, arr[:n], arr[n:])- A. 5 [0, 1, 2, 3, 4] [2, 3, 3, 4, 4]
- B. 5 [0, 1, 2, 3, 4] [2, 2, 3, 3, 4]
- C. 4 [0, 1, 2, 3] [2, 3, 3, 4, 4]
- D. 5 [0, 1, 2, 3, 4] [0, 0, 1, 1, 1]
Correct answer: B. 5 [0, 1, 2, 3, 4] [2, 2, 3, 3, 4]
The write index k moves only when a new value appears, and cells after k keep their old contents. So arr[5:] still shows 2, 2, 3, 3, 4 after the in-place pass.
More Python Programming questions
- What is printed? python def firsttrue(lo, hi, ok): while lo < hi: mid = (lo + hi) // 2 if ok(mid): hi = mid else: lo = mid + 1 return lo…
- 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))…