3.02 Data Abstractions HW
Popcorn Hack (Warm-up): Find the Level
Prediction: first level’s stars 10, second level’s stars 20, number of levels 3.
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]) # Python index 0 = first level
print(level_stars[1]) # Python index 1 = second level
print(len(level_stars)) # number of levels
Actual output: 10, 20, 3. Changing the first value to 14 changes only the first line, and the length stays 3 because replacing a value does not add a level.
Popcorn Hack (Graded): Extend the Level Sequence
Prediction: After replacing the second level with 12 and appending 5, the list is [6, 12, 12, 5] and the length is 4. AP index 2 selects the same second level as Python index 1.
Code Runner Challenge
UESL 3.2 Popcorn - Extend the level sequence
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Popcorn - Extend the level sequence
level_stars = [6, 10, 12]
level_stars[1] = 12 # replace the second level (AP index 2)
level_stars.append(5) # add one new level at the end
print(level_stars)
print(len(level_stars))
Actual output: [6, 12, 12, 5], then 4.
AP index: AP index 2 selects the same element as Python index 1.
Changed version: Appending 8 instead of 5 gives [6, 12, 12, 8], still four levels. Updating an element keeps the length the same because it replaces a value. Appending increases the length by one because it adds an element.
MCQ Check: Maker Decisions
| # | Question | My answer | Why |
|---|---|---|---|
| 1 | AP index of the first level | B (1) | AP lists start at 1. |
| 2 | Change in length when one count is replaced | A (0) | Replacing keeps the same number of elements. |
| 3 | What APPEND(level_stars, 5) does |
C (adds one element with value 5) | It does not touch level five or add five levels. |
| 4 | Why a list plus traversal for 100 levels | B (avoids a separate variable and sum edit for every level) | The benefit is easier maintenance, not guaranteed speed. |
| **MCQ 3.2: 4/4 | answers: B,A,C,B** |
Homework Hack: Grow Your UESL Game
level_stars = [12, 18, 10] holds each level’s collected stars in level order. I add 5 to the second level, append 7 for a new level, then use one traversal to print the collection, the level count, and the total. Expected: [12, 23, 10, 7], 4, 52.
Code Runner Challenge
UESL 3.2 Homework - Grow your UESL game
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Homework - Grow your UESL game
# Each element is one level's collected stars, in level order
level_stars = [12, 18, 10]
# Add 5 to the second level (skip if there is no second level)
if len(level_stars) > 1:
level_stars[1] = level_stars[1] + 5
# Append 7 for a brand new level
level_stars.append(7)
# Reset the total before EVERY calculation
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(level_stars)
print(len(level_stars))
print(total_stars)
Actual output: [12, 23, 10, 7], 4, 52.
Tests: Empty Collection
Prediction: With [] there are no levels to visit, so the loop never runs and the level count and total are both 0.
Code Runner Challenge
UESL 3.2 Test - Empty project
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Test - Empty project
level_stars = []
total_stars = 0
for stars in level_stars:
total_stars = total_stars + stars
print(len(level_stars))
print(total_stars)
Actual output: 0, 0. The same traversal works for zero, three, or four levels without editing it.
Extra check (second-level update guard):
Code Runner Challenge
UESL 3.2 Test - Guarded update on an empty list
View IPYNB Source
# CODE_RUNNER: UESL 3.2 Test - Guarded update on an empty list
level_stars = []
if len(level_stars) > 1:
level_stars[1] = level_stars[1] + 5 # skipped, otherwise IndexError
else:
print("No second level yet, skipping the bonus")
print(level_stars)
Actual output: No second level yet, skipping the bonus, then []. Without the guard, level_stars[1] would raise an IndexError.
Explanation: What a List Avoids
With 100 separate variables (level_one_stars, level_two_stars, …), every new level needs a new variable, and every total needs a new + level_101_stars edit to the calculation. Forget one and the total is silently wrong. With a list and a traversal, the collection is one name, the total calculation never changes, and the same code handles 3 levels, 100 levels, or none. The list removes those repeated edits, which is how abstraction manages complexity.
Connection to 3.1: Changing a list does not recalculate a previously stored total. The list organizes the state; assignment and the traversal are what update it.
Design Thinking
- Maker need: Add, reorder, or remove levels without losing track of each level’s stars.
- Goal: One understandable collection in level order that supports reading, updating, extending, and totaling.
- Representation considered: Separate variables versus one list. I chose a list of plain numbers so addition always makes sense.
- Prototype built: The
level_starslist with a guarded update, an append, and a single total traversal. - Test and revision: My first version updated
level_stars[1]without a guard and crashed on an empty project, so I addedif len(level_stars) > 1. - Suggestion: Give levels clear labels such as Forest, Cavern, Summit so players never need to understand indices.
Submission notes line: MCQ 3.2: 4/4 | answers: B,A,C,B