Checkpoint
Greedy and a little math
Twelve questions from both lessons, on inputs you have not met yet: greedy rules and the inputs that break them, the stock, run, jump and gas walks, bits and XOR, the lowest-1 loop, the missing number and Euclid's rule. Most take a minute with paper. The last one is a short program: Kadane's walk, extended to say where the best run is. Work each one out before you check it: a wrong answer here shows you which section to reread.
- Question 1 of 12.
Coins come in 7, 5 and 1, as many of each as you like. Biggest-first pays an amount by taking the biggest coin that still fits, again and again. What is the smallest amount it pays with more coins than it needs? Try amounts from 1 upward, and for each one ask whether fewer coins could pay it.
amountType a number: 12, -1, 0.5 and 3/4 all work.
- Question 2 of 12.
9 is
1001and 3 is0011in binary. Type exactly what this prints.a = 9 b = 3 print(a & b, a | b, a ^ b) print(a ^ 2, a ** 2, a >> 1) - Question 3 of 12.
For each list of prices, the program prints two answers: the highest price minus the lowest, then the lowest-price-so-far walk from the lesson. Type exactly what it prints.
for prices in ([6, 2, 8, 1, 3], [9, 7, 4, 1]): lowest = prices[0] best = 0 for price in prices: best = max(best, price - lowest) lowest = min(lowest, price) print(max(prices) - min(prices), best) - Question 4 of 12.
This loop counts the 1 bits of
xby turning off the lowest 1 on each pass:count = 0 while x > 0: x = x & (x - 1) count += 1How many passes does it make when
xstarts at 200?passesType a number: 12, -1, 0.5 and 3/4 all work.
- Question 5 of 12.
Each rule makes the best-looking choice at each step and never undoes it. Which rules always give a best answer? Pick every one that does. For each, try to break it on a tiny input; if you cannot, look for the exchange argument.
Choose every option that applies.
- Question 6 of 12.
numsholds the numbers 0 to 3 with one missing. The program XORs every number from 0 to 3, prints the result, then XORs in every item ofnumsand prints again. Type exactly what it prints.nums = [3, 0, 1] result = 0 for i in range(len(nums) + 1): result ^= i print(result) for x in nums: result ^= x print(result) - Question 7 of 12.
This version of Kadane's walk starts both numbers at 0 and walks every item. Type exactly what it prints.
def largest_run(nums): current = 0 best = 0 for x in nums: current = max(x, current + x) best = max(best, current) return best print(largest_run([2, -5, 3])) print(largest_run([-3, -1, -2])) - Question 8 of 12.
numsholds 9 different whole numbers, each from 0 to 9, so exactly one number in that range is missing. The items add up to 38. Which number is missing?missing numberType a number: 12, -1, 0.5 and 3/4 all work.
- Question 9 of 12.
In the jump game, each number is the longest jump allowed from that position, and the goal is to reach the last position from position 0. A tempting rule: from each position, make the longest jump allowed. On which list does that rule get stuck while
can_jumpfrom the lesson returnsTrue? - Question 10 of 12.
This is Euclid's loop from the lesson, with a print inside. The call passes the smaller number first. The last line steps 4 seats back from seat 0, on a ring of 6 seats numbered 0 to 5. Type exactly what it prints.
def gcd(a, b): while b > 0: a, b = b, a % b print(a, b) return a print(gcd(30, 105)) print((0 - 4) % 6) - Question 11 of 12.
Run
start_stationfrom the lesson on gas[1, 3, 2, 4]and cost[2, 2, 4, 1]. When the tank goes below zero,startmoves toi + 1and the tank empties. Which station does it return?startType a number: 12, -1, 0.5 and 3/4 all work.
- Question 12 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itWhere is the best run? Kadane's walk finds the largest sum of a run (one or more neighboring items). A common extension follow-up asks where that run is. Write
best_run(nums), which takes a non-empty list and returns[start, end], the first and last positions of a run with the largest sum. If several runs tie, any of them is accepted.For
[3, -4, 2, -1, 3]the answer is[2, 4]: 2 + (−1) + 3 = 4. Then press Watch it run.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.