Checkpoint
Counting steps: Big O
Eleven questions from all three lessons, most of them on code you have not seen. Work each one on paper before you answer: count the passes, name the case, and price every line. The last question asks you to write code, so run its tests before you submit. You pass with 80%.
- Question 1 of 11.
Type exactly what this prints. Count on paper before you check: the inner loop's range depends on
i, and//rounds down.steps = 0 for i in range(6): for j in range(i): steps += 1 print(steps) size = 40 halvings = 0 while size > 1: size = size // 2 halvings += 1 print(halvings) - Question 2 of 11.
numsis a list of n numbers andseenis a set. Which of these take O(n) time? Choose every one that does.Choose every option that applies.
- Question 3 of 11.
Plan A takes 1,000n steps on n items. Plan B takes n2/2 steps. At what n do the two plans take the same number of steps? Type a whole number.
nType a number: 12, -1, 0.5 and 3/4 all work.
- Question 4 of 11.
A teammate runs the every-pair duplicate check (the inner loop starts at
i + 1) on a list of 1,000 ids. The first two ids are equal, so it returnsTrueafter one comparison. The teammate says: "One comparison, so it is O(1)." What is the right reply? - Question 5 of 11.
This function returns the events newest first.
eventshas n items. What does it cost in time?def newest_first(events): result = [] for e in events: result.insert(0, e) return result - Question 6 of 11.
A problem's constraints say:
- 1 ≤ len(nums) ≤ 1,000
- −109 ≤ nums[i] ≤ 109
Your plan compares every pair of numbers once. Will it fit in about one second?
- Question 7 of 11.
How many times does the loop body run? Type a whole number.
n = 1000 size = 1 passes = 0 while size < n: size = size * 2 passes += 1passesType a number: 12, -1, 0.5 and 3/4 all work.
- Question 8 of 11.
readingsholds n sensor readings andthresholdsholds m thresholds. For each threshold, the function counts the readings above it. What is its time cost?def counts_above(readings, thresholds): result = [] for t in thresholds: above = 0 for r in readings: if r > t: above += 1 result.append(above) return result - Question 9 of 11.
Use the append model from the lesson on Python costs: the list starts with room for 1 item, and whenever an append finds it full, every stored item is copied into a block twice as big. Over 100 appends, how many items are copied in total? Type a whole number.
items copiedType a number: 12, -1, 0.5 and 3/4 all work.
- Question 10 of 11.
This function adds up a list with recursion. It moves an index instead of slicing, and it is called as
total(nums, 0). For a list of n numbers, what are its time and extra space?def total(nums, i): if i == len(nums): return 0 return nums[i] + total(nums, i + 1) - Question 11 of 11.
Run the tests before you submit. This question counts as correct if your latest run passes every check.
Code itViews from blocked users viewsis a list of user ids, one per page view, in time order.blockedis a list of blocked user ids. This function returns the views made by blocked users, in time order. It is correct but slow:def blocked_views(views, blocked): result = [] for user in views: if user in blocked: result.append(user) return resultWrite
blocked_views(views, blocked)so it runs in O(n + m) time, for n views and m blocked ids. Keep the result in time order. The last test gives it 30,000 views and 30,000 blocked ids with a 1 second limit, which the version above cannot meet: it takes several seconds.Write your code where the starter saysraise NotImplementedError, then press Run tests. Each check says what it expects.