ProgrammingHardTCS CodeVita · Infosys HackWithInfy
What is printed?
What is printed?
def flat(x):
for y in x:
if isinstance(y, list):
yield from flat(y)
else:
yield y
print(list(flat([1, [2, [3, [4]]], 5])))- A. [1, 2, 3, 4, 5]
- B. [1, 2, 3, 4, 6]
- C. [1, 2, 3, 4]
- D. [2, 3, 4, 5]
Correct answer: A. [1, 2, 3, 4, 5]
Each nested list is passed to a recursive generator with yield from, which passes every leaf value up. The depth here is only 4, so no recursion limit is reached.
More Python Programming questions
- What is printed? python print(len(str(int('7' 4300)))) try: int('7' 4301) except ValueError: print('no')
- What is printed? python print(round(0.25, 1), round(0.35, 1), round(2.5), round(-0.5))
- What is printed? python d = {'a': 1, 'b': 2} d['a'] = 3 d.pop('a') d['a'] = 4 print(list(d.items()))
- What is printed? python print({k % 4: k k for k in range(9, -1, -1)})
- What is printed? python print(sum([0.1] 10) == 1.0, 0.1 3 == 0.3)
- What is printed? python import heapq iv = sorted([(0, 30), (5, 10), (15, 20)]) h = [] for s, e in iv: if h and h[0] <= s…