Popcorn Hack

Code Runner Challenge

UESL 3.2 Warm-up - Find the level

View IPYNB Source
# CODE_RUNNER: UESL 3.2 Warm-up - Find the level
level_stars = [10, 20, 15]
print(level_stars[0])
print(level_stars[1])
print(len(level_stars))
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Prediction: 10, 20, 3
Actual Output:

10

20

3

Change Explanation: Changing the first value from 10 to 14 updates the first output to 14 because it replaces the element at index 0. The length stays 3 because replacing an existing element does not add or remove elements from the list.

MCQ

Answers: B, A, C, B
Score: 4/4
Which AP index selects the first level? Answer: B (1) — AP CSP pseudocode uses 1-based indexing.

Replacing one count changes the list length by: Answer: A (0) — Overwriting an existing value does not alter total count.

What does APPEND(level_stars, 5) do?

Answer: C (Add one element with value 5) — Appends a single item to the end of the list.

Why use a list plus traversal for 100 levels?

Answer: B (It avoids a separate variable and sum edit for every level) — Reduces program complexity by avoiding redundant code.

Homework

Code Runner Challenge

UESL 3.2 - Homework Solution

View IPYNB Source
# CODE_RUNNER: UESL 3.2 - Homework Solution
# Collection representing stars collected in level order
level_stars = [12, 18, 10]

# Add 5 to the second level (index 1)
level_stars[1] = level_stars[1] + 5

# Append 7 stars for a new level
level_stars.append(7)

# Traversal using Example C pattern
total_stars = 0
for stars in level_stars:
  total_stars = total_stars + stars

print(f"Collection: {level_stars}")
print(f"Level Count: {len(level_stars)}")
print(f"Total Stars: {total_stars}")
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Tests

Test 1: Standard Game Flow

Code Runner Challenge

UESL 3.2 - Test 1 Standard Flow

View IPYNB Source
# CODE_RUNNER: UESL 3.2 - Test 1 Standard Flow
level_stars = [12, 18, 10]

# Guard check: update second level if it exists
if len(level_stars) >= 2:
  level_stars[1] = level_stars[1] + 5

# Append new level
level_stars.append(7)

total_stars = 0
for stars in level_stars:
  total_stars = total_stars + stars

print(level_stars)
print(len(level_stars))
print(total_stars)
Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

Actual Output:

[12, 23, 10, 7]
4
52


Explanation:

The initial list [12, 18, 10] is modified by adding 5 to index 1 (changing 18 to 23). Appending 7 adds a 4th level to the list. The for loop iterates through all 4 elements to calculate a final sum of 52.

Test 2: Empty Game State ([])

Code Runner Challenge

UESL 3.2 - Test 2 Empty List

View IPYNB Source
# CODE_RUNNER: UESL 3.2 - Test 2 Empty List
level_stars = []

# Guard check prevents IndexError when list is empty
if len(level_stars) >= 2:
  level_stars[1] = level_stars[1] + 5

total_stars = 0
for stars in level_stars:
  total_stars = total_stars + stars

print(len(level_stars))
print(total_stars)

Lines: 1 Characters: 0
Output
Click "Run" in code control panel to see output ...

0 0


Because level_stars is initialized as empty, the length check (len(level_stars) >= 2) fails and skips the index update, avoiding a crash (IndexError). The traversal loop runs 0 times, correctly reporting 0 levels and 0 total stars.

Complexity Reduction Explanation

Using 100 separate variables (level1 = 10, level2 = 20, …, level100 = 15) requires writing 100 distinct variable declarations and updating a hardcoded addition statement (total = level1 + level2 + … + level100) every time a level is added or removed.
With data abstraction using a list and traversal loop: All values are stored inside a single structure (level_stars). A single for loop automatically handles any number of levels dynamically. Adding, removing, or modifying levels requires no structural changes to the calculation logic.

Design Thinking

Player/Maker Need: The maker needs an easy way to expand game levels without rewriting total-calculation logic. The player needs clear feedback on total progress across all levels.
Goal: Use list abstraction to eliminate manual variable maintenance and maintain an accurate total star count dynamically.
Representation Considered: Storing star counts in individual variables (l1, l2, l3) vs. a single list (level_stars). A list was chosen because it supports dynamic iteration via loops.
Prototype: level_stars = [12, 18, 10] updated dynamically with append() and modified via index assignment level_stars[1] += 5.
Test & Revision:

Issue: Running index updates on an empty list ([]) causes an IndexError.

Revision: Added a length guard (if len(level_stars) >= 2:) before attempting to modify index 1, ensuring safety across all game states.

Player UI Improvement: Present levels with human-readable labels (e.g., “Level 1: Forest”, “Level 2: Cavern”) in the UI so players don’t need to understand 0-based technical indices.