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))- A. [1, 1, 0] [0]
- B. [1, 0, 1] []
- C. [1, 1, 0] []
- 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
- What is printed? python print(divmod(7, -3), 7 % -3, -7 % 3, -7 // 3)
- What is printed? python x = 'aaa' y = list(x) y[0] = 'b' print(x, ''.join(y), x is y)
- What is printed? python grid = [[1, 2], [3, 4]] flat = [v for row in grid for v in row] print(flat, sum(flat) == sum(map(sum, grid)))
- What is printed? python def merge(a, b): out, i, j = [], 0, 0 while i < len(a) and j < len(b): if a[i] <= b[j]: out.append(a[i]); i += 1…
- Which change removes the RecursionError for a DFS over a path of 10^5 nodes, while keeping the same visiting order?
- What is printed? python words = ['apple', 'bob', 'kayak', 'tree'] print([w for w in words if w == w[::-1]], len(words[1:3]))