Back to Sessions

Session 7

3 lessons completed

Find an Item in a List

Completed

Assignment

Complete contains_leather_scraps using no-index syntax. Iterate over items. If you find 'Leather Scraps', set found = True.

Solution

def contains_leather_scraps(items):
    found = False
    for leather in items:
        if leather == "Leather Scraps":
            found = True
    return found

Concept

for item in list syntax, loop variable naming for readability

How it went

Solved in ~2 minutes after initial hesitation on loop variable naming.

Ratings

Concept grasp: Solid
Time to solve: ~2 minutes

Key takeaways

  • Loop variable names can be meaningful: 'for leather in items' reads better than 'for x in items'

Check Character Levels

Completed

Assignment

Complete check_character_levels. Loop over indexes. If old_character_levels[i] < new_character_levels[i], print i.

Solution

def check_character_levels():
    old_character_levels = [1, 42, 43, 53, 12, 3, 32, 34, 54, 32, 43]
    new_character_levels = [1, 42, 45, 54, 12, 3, 32, 38, 54, 32, 42]
    for i in range(0, len(old_character_levels)):
        if old_character_levels[i] < new_character_levels[i]:
            print(i)

Concept

Index-based list comparison, tracking changes between lists

How it went

Solved correctly, understood the index comparison pattern.

Ratings

Concept grasp: Solid
Pattern recognition: Good

Key takeaways

  • Pattern recognition: if two lists need comparing element-by-element, track the indices

Find Max

Completed

Assignment

Complete find_max. Start max_so_far = float('-inf'). Compare each number to max_so_far. If larger, replace. Return max_so_far. Return float('-inf') if list is empty.

Solution

def find_max(nums):
    max_so_far = float("-inf")
    for num in nums:
        if max_so_far < num:
            max_so_far = num
    return max_so_far

Concept

Accumulator pattern, float('-inf') as sentinel value

How it went

Struggled with float('-inf') as a starting point. Breakthrough achieved after Socratic questioning. Applied max_so_far < num without being told — chose comparison direction that reads most naturally.

Ratings

Concept grasp: Developing — breakthrough after Socratic questioning
Pattern application: Good — applied max_so_far < num unprompted

Key takeaways

  • The -inf trick: when finding the biggest thing in a set and you don't know what's in it, start at float('-inf') so the first real value always wins