Checkpoint
Dynamic programming
Eleven questions from the whole module, on inputs you have not met yet: recurrences and what a memo costs, one-dimensional tables, grids and two strings, choices with a budget, and finding the state. Most take a few minutes with paper. Two are short programs: routes around blocked cells, and the fewest numbers when each number may be used once. Work each one out before you check it: a wrong answer here shows you which lesson to reread.
- Question 1 of 11.
A staircase has 6 steps, and each stride climbs 1 step or 3 steps. Order matters, as in climbing stairs:
[1, 3]and[3, 1]are different climbs. How many different climbs reach step 6? Type a whole number.climbsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 2 of 11.
fewest_jumps(nums)returns the fewest jumps from position 0 to the last position of a list of n numbers. From positioniyou may jump forward 1 up tonums[i]places, and everynums[i]is between 1 and n.def fewest_jumps(nums): memo = {} def jumps(i): if i >= len(nums) - 1: return 0 if i in memo: return memo[i] best = float("inf") for step in range(1, nums[i] + 1): best = min(best, 1 + jumps(i + step)) memo[i] = best return best return jumps(0)What is its worst-case time?
- Question 3 of 11.
This is the decode ways function from the one-dimensional lesson (A is 1, up to Z as 26). Type exactly what the three
printlines show.def num_decodings(s): n = len(s) dp = [0] * (n + 1) dp[0] = 1 for i in range(1, n + 1): if s[i - 1] != "0": dp[i] += dp[i - 1] if i >= 2 and 10 <= int(s[i - 2:i]) <= 26: dp[i] += dp[i - 2] return dp[n] print(num_decodings("1203")) print(num_decodings("100")) print(num_decodings("226")) - Question 4 of 11.
Inserts, deletes and replaces each cost 1. What is the edit distance from
"sort"to"rots"? Fill the 5 by 5 table if you need it: row 0 and column 0 hold 0 to 4. Type a whole number.editsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 5 of 11.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itRoutes around the shelves The warehouse grid from the grids lesson now has shelves.
grid[r][c]is 1 for a shelf, which the cart may not enter, and 0 for an open cell. The cart starts in the top-left cell and moves one cell right or one cell down at a time.Write
routes_around_shelves(grid), the number of routes to the bottom-right cell. If either end is a shelf, the answer is 0. For[[0, 0, 0, 0], [0, 1, 0, 0], [0, 0, 0, 0]]the answer is 4: an open 3 by 4 grid has 10 routes, and 6 of them pass through the shelf. Then press Watch it run.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects. - Question 6 of 11.
The longest palindromic subsequence table has one cell per piece
s[i..j], from positionito positionjwith both included. Fori < j, the cellbest[i][j]reads three cells:best[i + 1][j - 1],best[i + 1][j]andbest[i][j - 1]. The cells withi == j(one letter) are base cases, and an empty piece counts 0. Which fill orders have every cell's inputs ready when it is filled?Choose every option that applies.
- Question 7 of 11.
The same counting loop runs twice on the numbers
[2, 3]and target 6: once walking the sums downward, once upward. Type exactly what it prints.def count(nums, target, downward): ways = [1] + [0] * target for x in nums: if downward: sums = range(target, x - 1, -1) else: sums = range(x, target + 1) for s in sums: ways[s] += ways[s - x] return ways[target] print(count([2, 3], 6, True)) print(count([2, 3], 6, False)) - Question 8 of 11.
Fewest coins, in the one-dimensional lesson, put the amounts in the outer loop and the coins inside. Coin combinations, in the budget lesson, must put the coin values outside, or it counts 1 + 2 and 2 + 1 separately. Why is amounts-outside safe for fewest coins?
- Question 9 of 11.
What is the length of the longest strictly increasing subsequence of
[2, 5, 5, 7, 1]? Each wrong option is a slip worth recognizing. - Question 10 of 11.
House robber with one more rule: at most
khouses may be robbed in total, and still never two neighbors. You write a recursionbest(i, ...), the most cash from housesito the end of the street. Robbing houseimoves the recursion toi + 2; skipping it moves it toi + 1. Which arguments make the right state? - Question 11 of 11.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itFewest numbers, each used once Write
fewest_numbers(nums, target): the fewest numbers fromnumsthat add up to exactlytarget, each number used at most once, or -1 if no group does. The numbers are positive, and equal values at different positions are different numbers. A target of 0 needs no numbers, so it gives 0.For
[5, 4, 4, 1, 1, 1]and target 8 the answer is 2 (4 + 4). Biggest first takes the 5 and then needs all three 1s: 4 numbers.This joins two lessons: the fewest-coins cell (a minimum that starts at infinity) and the 0/1 knapsack walk (each item at most once). Then press Watch it run.
Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.