ProgrammingHardCognizant GenC · TCS NQT
What is printed?
What is printed?
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 = prev, h, h.nxt
return prev
h = N(1, N(2, N(3)))
r = rev(h)
out = []
while r:
out.append(r.v)
r = r.nxt
print(out)- A. [3, 2, 1]
- B. [1, 2, 3]
- C. [2, 3, 1]
- D. [3]
Correct answer: A. [3, 2, 1]
The tuple assignment evaluates its right side first, so each node's next pointer is redirected before the walk moves on. The list comes out reversed as 3, 2, 1.
More Python Programming questions
- 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…
- What is printed? python from collections import deque d = deque([1, 2, 3, 4, 5], maxlen=5) d.rotate(2) d.appendleft(0) print(list(d)…
- What is printed? python print(max(['bb', 'aa', 'c'], key=len), min(['bb', 'aa', 'c'], key=len))
- What is printed? python from collections import defaultdict words = ['eat', 'tea', 'tan', 'ate', 'nat', 'bat'] g = defaultdict(list) for w…
- What is printed? python print(' a b '.split(' '), ' a b '.split(), 'a b x c d'.split(None, 1))