ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def chunks(xs, k):
for i in range(0, len(xs), k):
yield xs[i:i + k]
def flatten(seq):
for s in seq:
if isinstance(s, list):
yield from flatten(s)
else:
yield s
print(list(chunks("abcdefg", 3)), list(flatten([1, [2, [3, []], "45"]])))- A. ['abc', 'def', 'g'] [1, 2, 3, '45']
- B. ['abc', 'def'] [1, 2, 3, '45']
- C. ['abc', 'def', 'g'] [1, 2, 3, '4', '5']
- D. [['a', 'b', 'c'], ['d', 'e', 'f'], ['g']] [1, 2, 3, '45']
Correct answer: A. ['abc', 'def', 'g'] [1, 2, 3, '45']
Slicing a string yields strings, and the last chunk is shorter. flatten recurses only into lists, so the empty list contributes nothing and the string '45' is yielded whole.
More Python Programming questions
- What is printed? (Output lines are shown separated by " / ".) python def f(a, b, /, c, , d): return (a, b, c, d) args = (1, 2) kw = {"c"…
- What is printed? python class A: def init(self, kw): print("A", sorted(kw), end=" ") class B(A): def init(self, b=0, kw): print("B", end="…
- What is printed? python class Base: def run(self): return self.step() def step(self): return "base" class Child(Base): def step(self)…
- What is printed? python class Meta(type): def new(m, name, bases, ns): ns["kind"] = name.upper() return super().new(m, name, bases, ns)…
- What is printed? python class S: slots = ("a",) class T(S): pass t = T() t.a = 1 t.b = 2 s = S() try: s.b = 2 except AttributeError…
- What is printed? python class Account: rate = 0.05 def init(self, bal): self.bal = bal def interest(self): return self.bal self.rate a =…