ProgrammingHardTCS NQT · Wipro pattern
What is printed?
What is printed?
def paths(g):
R, C = len(g), len(g[0])
dp = [[0] * C for _ in range(R)]
dp[0][0] = 0 if g[0][0] else 1
for r in range(R):
for c in range(C):
if g[r][c]:
dp[r][c] = 0
elif r == 0 and c == 0:
continue
else:
dp[r][c] = (dp[r-1][c] if r else 0) + (dp[r][c-1] if c else 0)
return dp[-1][-1]
print(paths([[0, 0, 0], [0, 1, 0], [0, 0, 0]]), paths([[0, 1], [0, 0]]))- A. 2 2
- B. 3 1
- C. 2 1
- D. 1 1
Correct answer: C. 2 1
The DP counts right-or-down paths that avoid blocked cells. The 3x3 grid with a blocked centre has 2 paths, and the 2x2 grid with a blocked top-right cell has 1.
More Python Programming questions
- What is printed? python def cool(p): hold, sold, rest = float('-inf'), 0, 0 for x in p: hold, sold, rest = max(hold, rest - x), hold + x…
- What is printed? python import bisect def inv(a): s, c = [], 0 for x in a: c += len(s) - bisect.bisectright(s, x) bisect.insort(s, x)…
- What is printed? python import bisect a = [10, 20, 20, 30] print([bisect.bisectleft(a, x) for x in (20, 25, 5, 40)])
- What is printed? python from functools import lrucache @lrucache(maxsize=None) def grid(r, c): if r == 0 or c == 0: return 1 return grid(r…
- What is printed? python def cancomplete(gas, cost): total = tank = start = 0 for i in range(len(gas)): d = gas[i] - cost[i] total += d…
- What is printed? python x = 0b101100 print(x & (x - 1), x & -x, bin(x).count('1'))