Checkpoint
Arrays and hash maps
Twelve questions from the whole module, on inputs you have not met yet: list costs, buckets and keys, counting and seen-before, grouping keys, and prefix sums. Most take a minute with paper. The last one is a short program that joins two sum's dict to prefix sums. Work each one out before you check it: a wrong answer here shows you which lesson to reread.
- Question 1 of 12.
Each line runs on a list
numsof n items. Which one is O(n2)? - Question 2 of 12.
This function is meant to return the first character that appears a second time, or
Noneif no character repeats. Type exactly what the two calls print.def first_repeat(s): seen = set() for ch in s: seen.add(ch) if ch in seen: return ch return None print(first_repeat("abcb")) print(first_repeat("xyz")) - Question 3 of 12.
A chained hash table like the lesson's holds small whole numbers, so
hash(key)is the key itself. It has 8 buckets, and 27 sits in bucket 27 % 8 = 3. Then the table doubles to 16 buckets and places every key again. Which bucket holds 27 now? Type a whole number from 0 to 15.bucketType a number: 12, -1, 0.5 and 3/4 all work.
- Question 4 of 12.
prefixis built with a 0 in front, soprefix[i]is the sum of the firstiitems. Which expression is the sum ofnums[3]throughnums[6], both included? - Question 5 of 12.
You group lists of numbers in a dict of lists. Two lists belong together when they hold the same numbers, each the same number of times, in any order:
[3, 1, 3]belongs with[3, 3, 1], but not with[1, 3, 1]or[1, 3]. Which key does that and can be stored as a dict key? - Question 6 of 12.
A list holds 9 items, at positions 0 to 8.
nums.pop(2)removes the item at position 2 and keeps the list in order with no gaps. How many items move? A move is one item shifted from one slot to another.movesType a number: 12, -1, 0.5 and 3/4 all work.
- Question 7 of 12.
This is the one-walk two sum from the counting lesson, run on two lists with repeated numbers. Type exactly what it prints.
def two_sum(nums, target): seen = {} for i, x in enumerate(nums): partner = target - x if partner in seen: return [seen[partner], i] seen[x] = i print(two_sum([3, 9, 5, 3], 6)) print(two_sum([1, 4, 4], 8)) - Question 8 of 12.
dis a dict andsis a set. Which of these lines raise aTypeError? Pick every one that does.Choose every option that applies.
- Question 9 of 12.
The longest-run function from the grouping lesson runs on
nums = [10, 4, 11, 5, 20, 6, 12]. It skips everyxwhosex - 1is in the set. How many values ofxstart a run and measure it with thewhileloop?startsType a number: 12, -1, 0.5 and 3/4 all work.
- Question 10 of 12.
How many subarrays of
[1, -1, 0]add up to 0? A subarray is one or more neighboring items. Count them by hand, then check your count with the prefix sum walk: a dict of running totals that starts as{0: 1}, looking uptotal - kbefore recordingtotal.subarraysType a number: 12, -1, 0.5 and 3/4 all work.
- Question 11 of 12.
Type exactly what this prints.
w[0]is the first letter ofw.from collections import defaultdict groups = defaultdict(list) for w in ["ant", "bee", "art"]: groups[w[0]].append(w) print(len(groups)) if groups["c"]: print("found c words") print(len(groups)) print("z" in groups) - Question 12 of 12.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itThe longest stretch that adds up to k Write
longest_with_sum(nums, k), which returns the length of the longest subarray (one or more neighboring items) that adds up to exactlyk, or 0 if none does. The numbers can be negative. For[2, 1, -1, 3, -2]with k = 2 the answer is 3:[2, 1, -1].Use one walk with a running total and a dict, as two sum does, but store each running total with the first position where it appeared. Then press Watch it run.
Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.