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]))- A. 20 16
- B. 19 9
- C. 20 9
- 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
- What is printed? python def most2(s): cnt = {} l = best = 0 for r, ch in enumerate(s): cnt[ch] = cnt.get(ch, 0) + 1 while len(cnt) > 2…
- What is printed? python class Bag: items = [] def add(self, x): self.items.append(x) a, b = Bag(), Bag() a.add(1) b.add(2) print(a.items…
- What is printed? python print(max('bca'), min('bca', key=lambda c: (c, -ord(c))))
- What is printed? python from collections import deque d = deque([1, 2, 3]) d.extendleft([4, 5]) print(list(d))
- What is printed? python x = 'abc' print(x.replace('', '-'))
- What is printed? python try: 'abc'.split('') except ValueError as e: print(e)