ProgrammingVery hardCodeforces-style · placement coding round

What is printed?

What is printed?
def grid_paths(r, c, memo={}):
    if r == 0 or c == 0:
        return 1
    if (r, c) not in memo:
        memo[(r, c)] = grid_paths(r - 1, c) + grid_paths(r, c - 1)
    return memo[(r, c)]
print(grid_paths(3, 3), len(grid_paths.__defaults__[0]))
  1. A. 20 16
  2. B. 19 9
  3. C. 20 9
  4. D. 20 15

Correct answer: C. 20 9

Only pairs with both coordinates at least 1 are stored, so the default dict holds 9 entries. The number of monotone paths to (3, 3) is C(6, 3) = 20.

More Python Programming questions

Exams that ask this topic