🎯Calibrate
1Fill-in
2Debug
3Extend
4Create
🧠Concept
📓Reflect
👤 Your Details REQUIRED — fill in before you begin (★ = required)
🎯 Before You Begin Calibration Check 2 questions + 1 reflection — unlocks Tier 1
Think about why checking membership in a set is faster than in a list. What does a set store, and what operations change it?
1 10 5
Tier 1 Fill in the Blanks Complete the code skeleton — 8 blanks

Complete the set-based collision system. Fill each blank with the correct set operation or data structure expression.

# Build a set for O(1) membership testing
snake_set = ▢1(snake)

# In the update step:
snake.insert(0, new_head)
snake_set.▢2(new_head)

if not ate_food:
    tail = snake.pop()
    snake_set.▢3(tail)   # remove tail from set

# Self-collision check (O(1)):
body_set = set(snake[▢4:])     # exclude head (index 0)
alive = new_head ▢5 body_set

# Score with high-score tracking:
score = ▢6
if ate_food:
    score["player"] ▢7= 1
    if score["player"] > score[▢8]:
        score["high"] = score["player"]
1.Blank 1: s__
2.Blank 2: a__
3.Blank 3: d______
4.Blank 4: _
5.Blank 5: n_____
6.Blank 6: "_____________________
7.Blank 7: _
8.Blank 8: "_____
Tier 2 Find & Fix the Bugs 3 bugs to correct

The set-based code below has 3 bugs. Type the corrected line for each.

snake_set = set(snake)
snake.insert(0, new_head)
snake_set.add(new_head)

if not ate_food:
    tail = snake.pop()
    snake_set.remove(tail)       # Bug 1 – can raise KeyError

body_set = set(snake)            # Bug 2 – includes head
alive = new_head not in body_set

score["player"] + 1              # Bug 3 – doesn't save the result

Bug 1

Buggy line:

snake_set.remove(tail)

Your fix:

Hint: `remove()` raises KeyError if the element is missing. Which set method silently ignores a missing element?

Bug 2

Buggy line:

body_set = set(snake)

Your fix:

Hint: `set(snake)` includes the head at index 0. Collision with your own head would always end the game immediately.

Bug 3

Buggy line:

score["player"] + 1

Your fix:

Hint: `+` computes the sum but discards it. Use `+=` to update the value stored in the dictionary.

Reflection 2 Mid-Point Check-In
Tier 3 Extend the Code Add the feature described below

Write a function `check_alive(snake, new_head, wall_death)` that returns `True` if the snake is alive after the move. It should check self-collision using a set AND optionally wall collision.

def check_alive(snake, new_head, wall_death=False):
    hx, hy = new_head
    # Check wall collision if wall_death is True
    # YOUR CODE here

    # Check self-collision using a set of body positions (exclude head)
    # YOUR CODE here

    return True  # replace with correct logic
Tier 4 Create from Scratch Open-ended challenge

**Performance experiment:** Write a script that creates a list of 1 000 (x, y) tuples, then times 100 000 membership checks using both a list and a set. Print the ratio. Paste your timing code and results.

🧠 Concept Check Test Your Understanding 5 quick questions — required before the Reflective Journal 🔒

Answer all 5 questions, then the Reflective Journal will unlock.

1. snake = [(5,5),(4,5),(3,5)] — what is snake[1]?

2. How does the snake appear to move each frame?

3. snake.insert(0, new_head) — what does this do?

4. What does len(snake) tell you?

5. snake.pop() removes:

📓 Reflective Journal 5 prompts — required before Step 7 is marked complete 🔒

Answer each prompt thoughtfully. There are no right or wrong answers — this is your own reflection.

🎯 Post-Activity Check-In After Completing All Tiers Complete after the Reflective Journal to finish this step 🔒
1. How confident do you feel about this step’s concepts now? (1 = not confident, 10 = very confident)
1 10 5
2. Your Tier 1 first-attempt score (auto-filled)
%