ProgrammingHardGATE CS · Product-company placement
What is printed?
What is printed?
from functools import reduce print(reduce(lambda a, b: a * 10 + b, [1, 2, 3]), reduce(lambda a, b: a + b, [], 7), reduce(max, [3, 9, 2]))
- A. 6 7 9
- B. 123 0 9
- C. 123 7 9
- D. 321 7 9
Correct answer: C. 123 7 9
reduce folds left: ((1*10 + 2)*10 + 3) = 123. With an empty sequence it returns the initializer 7. reduce(max, ...) keeps the larger value at each step.
More Python Programming questions
- What is printed? python from functools import lrucache calls = 0 @lrucache(maxsize=2) def f(x): global calls calls += 1 return x for v in…
- What is printed? (Output lines are shown separated by " / ".) python from functools import partial def power(base, exp): return base exp…
- What is printed? python x = [3, 1, 2] y = sorted(x) z = reversed(x) x.append(0) print(y, list(z))
- What is printed? python m = map(lambda v: v 2, [1, 2, 3]) f = filter(None, [0, 1, 2]) print(list(m), list(m), next(f), list(zip(f, "xyz")))
- What is printed? python a = [1, 2, 3] for i, v in enumerate(a): a[i] = v 2 if i == 0: a.append(9) if len(a) > 4: break print(a)
- What is printed? python x = 0 def f(): global x x += 1 return x d = {f(): f(), f(): f()} L = [f(), f()] L[f() - 7] = f() print(d, L)