ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
lst = list(range(5))
lst[1:4] = []
try:
lst[::2] = [9, 9]
except ValueError:
print('ValueError', lst)- A. ValueError [0, 9]
- B. ValueError [0, 4]
- C. ValueError [9, 9]
- D. [9, 9]
Correct answer: B. ValueError [0, 4]
After removing indices 1 to 3 the list is [0, 4]. Its extended slice [::2] has only one position, index 0, so assigning two values raises ValueError, and the list is left unchanged.
More Python Programming questions
- What is printed? python 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…
- 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…