ProgrammingModerateCodeforces-style · placement coding round
What is printed?
What is printed?
from functools import reduce import operator a = [4, 1, 2, 1, 2] print(reduce(operator.xor, a), reduce(operator.xor, a[1:], 0), reduce(lambda x, y: x ^ y, [], 7))
- A. 4 0 7
- B. 4 1 7
- C. 0 0 7
- D. 4 0 0
Correct answer: A. 4 0 7
XOR cancels equal pairs, so 1, 2, 1, 2 cancel to 0 and the single 4 remains. Folding an empty list with initial value 7 returns 7.
More Python Programming questions
- What is printed? python def spf(n): s = list(range(n + 1)) for i in range(2, int(n 0.5) + 1): if s[i] == i: for j in range(i i, n + 1, i)…
- What is printed? python def row(n): r = [1] for in range(n): r = [1] + [r[i] + r[i + 1] for i in range(len(r) - 1)] + [1] return r…
- What is printed? python def pairs(a, t): a = sorted(a) l, r = 0, len(a) - 1 out = [] while l < r: s = a[l] + a[r] if s == t…
- What is printed? python class N: def init(self, v, nxt=None): self.v, self.nxt = v, nxt def rev(h): prev = None while h: h.nxt, prev, h =…
- What is printed? python def cntpal(s): n = len(s) c = 0 for ctr in range(2 n - 1): l, r = ctr // 2, ctr // 2 + ctr % 2 while l >= 0 and r…
- What is printed? python import bisect a = [9, 7, 5, 3, 1] print(bisect.bisectleft(a, -6, key=lambda v: -v), bisect.bisectleft(a, 6…