Algorithmic Efficiency HW
Popcorn
Warm-up (practice):
Code Runner Challenge
Library 3.17 Warm-up - Find the book
View IPYNB Source
# CODE_RUNNER: Library 3.17 Warm-up - Find the book
linear_checks = 5
binary_checks = 3
print(linear_checks)
print(binary_checks)
print(linear_checks - binary_checks)
Checkpoint B (graded):
Code Runner Challenge
Library 3.17 Popcorn - Expand the catalog (n = 16)
View IPYNB Source
# CODE_RUNNER: Library 3.17 Popcorn - Expand the catalog (n = 16)
# Library 3.17 Popcorn - Expand the catalog (edited: n changed from 8 to 16)
import math
n = 16
linear_checks = n
binary_checks = 0 if n == 0 else math.floor(math.log2(n)) + 1
print(n)
print(linear_checks)
print(binary_checks)
Language: Python
Prediction: For 8 books, linear search will need 8 checks. Binary search will need 4 checks. At 16 books, linear should double to 16 and binary should go up by one to 5.
Actual Output (n = 16):
16165
Recommendation: When n doubles (8 to 16), linear checks double, but binary checks only increase by 1. Use binary search as the catalog grows (as long as the catalog is sorted).
MCQ
MCQ 3.17: 4/4 | answers: A, B, B, C
Homework
Language: Python
Code Runner Challenge
Library 3.17 Homework - Compare catalog growth
View IPYNB Source
# CODE_RUNNER: Library 3.17 Homework - Compare catalog growth
def check_growth(n):
visits = n
pairs = n * n
print(f"Books: {n}")
print(f"Visits (Linear): {visits}")
print(f"Pairs (Quadratic): {pairs}\n")
check_growth(12)
check_growth(24)
Prediction:
- For n = 12: expect 12, 12, 144.
- For n = 24: expect 24, 24, 576.
Actual Output (n = 12):
Books: 12Visits (Linear): 12Pairs (Quadratic): 144
Actual Output (n = 24):
Books: 24Visits (Linear): 24Pairs (Quadratic): 576
Explanation of Growth: Visits grow linearly (doubling 12 to 24 doubles visits). Pairs grow quadratically (doubling n quadruples the pairs from 144 to 576).
Recommendation (1,000 Books): For 1,000 books, linear search takes 1,000 checks (worst case); binary search takes only 10. If the catalog is sorted, use binary search. If it is unsorted, you must use linear search or sort it first (which requires extra work or a separate stored copy).
Tests
Main Output (n=12):
Books: 12Visits (Linear): 12Pairs (Quadratic): 144
Empty Catalog Output (n=0):
Books: 0Visits (Linear): 0Pairs (Quadratic): 0
Growth Explanation: Visits equal n, so doubling the catalog from 12 to 24 doubles visits (12 → 24). Pairs equal n × n, so doubling n multiplies pairs by 2 × 2 = 4 (144 → 576). With no books, there is nothing to visit or pair, so every count is 0.
Code Runner Challenge
Library 3.17 Test - Empty catalog
View IPYNB Source
# CODE_RUNNER: Library 3.17 Test - Empty catalog
# Test: empty catalog (fresh start, n = 0)
def check_growth(n):
visits = n
pairs = n * n
print(f"Books: {n}")
print(f"Visits (Linear): {visits}")
print(f"Pairs (Quadratic): {pairs}")
check_growth(0)
Design Thinking
Librarian Need: Needs to see how task times scale up, using clear labels instead of raw math output.
Goal: Model linear vs. quadratic growth clearly.
Methods Considered: Linear search (works on any catalog, but 1,000 checks for 1,000 books) vs. binary search (only 10 checks, but the catalog must be sorted). I used a Python function so the same calculation runs for any n.
Prototype: A basic script printing just numbers.
Actual Test/Revision: I revised the prototype to use f-strings so the output explicitly says “Visits (Linear):” and “Pairs (Quadratic):” for clarity.