ProgrammingVery hardGATE CS · Product-company placement
What is printed?
What is printed?
def f(n):
if n == 0:
return []
return [n] + f(n - 1) if n % 2 else f(n - 1) + [n]
print(f(5))- A. [5, 3, 1, 2, 4]
- B. [5, 4, 3, 2, 1]
- C. [1, 2, 3, 4, 5]
- D. [4, 2, 1, 3, 5]
Correct answer: A. [5, 3, 1, 2, 4]
Odd n is prepended and even n appended around f(n-1): f(1) = [1], f(2) = [1, 2], f(3) = [3, 1, 2], f(4) = [3, 1, 2, 4], f(5) = [5, 3, 1, 2, 4].
More Python Programming questions
- What is printed? python x = 1 def f(): print(x, end=" ") def g(): x = 2 f() g() h = lambda: x x = 3 print(h())
- What is printed? python def outer(): v = "a" def inner(): return v v = "b" r1 = inner() del v try: return r1, inner() except NameError as…
- What is printed? python print(eval("[i for i in range(3)]"), [x for x in "ab"] 2, list(map(str.upper, "ab")), {"aba"} == {"a", "b"})
- What is printed? python class A: def init(self): self.v = 1 def eq(self, o): return isinstance(o, A) and self.v == o.v hash = None class…
- What is printed? python print(isinstance(True, int), issubclass(bool, int), type(True) is int, True == 1, hash(True) == hash(1.0))
- What is printed? python s = {1, 2, 3} s.add(2) s.discard(9) s.update([3, 4], (5,)) r = s.pop() if False else len(s) try: s.remove(9)…