ProgrammingVery hardTCS CodeVita · Infosys HackWithInfy

What is printed?

What is printed?
def solve(n):
    if n == 0:
        return []
    return solve(n // 2) + [n % 2]
print(solve(6), solve(0))
  1. A. [1, 1, 0] [0]
  2. B. [1, 0, 1] []
  3. C. [1, 1, 0] []
  4. D. [0, 1, 1] []

Correct answer: C. [1, 1, 0] []

solve(6) gets [1, 1] from solve(3) then appends 0, and so on. The bits come out most significant first. solve(0) is the empty list.

More Python Programming questions

Exams that ask this topic