Indexing in Action¶

👨🏾‍🎨 Where's Waldo¶

You have a list of names. Write a function that returns the index of 'Waldo'. Return None if Waldo is not present.

waldo.py¶

In [ ]:
def wheres_waldo(names):
    for index in range(len(names)):
        if names[index] == 'Waldo':
            return index
    return None

wheres_waldo(['Juan','William','Wally','Waldo','Wilford','Winston'])

🧑🏻‍🎨 Middles¶

Find all positions in a list where the value is greater than the value on the left and less than the value on the right.

The first and last positions do not have neighbors on both sides and should not be part of the output.

In [ ]:
numbers = [1, 5, 3, 7, 8, 2, 3, 4, 6, 9, 0, 2, 7]

def find_middle_positions(items):
    return []

find_middle_positions(numbers)

NOTES

  • Draw it out
    • How do we know a position is a "middle"
    • Boundary conditions: what do we do for the first and last position?
      • How does this affect our strategy?
In [ ]:
numbers = [1, 5, 3, 7, 8, 2, 3, 4, 6, 9, 0, 2, 7]

def find_middle_positions(items):
    middles = []
    for index in range(1, len(items) - 1):
        if items[index] > items[index - 1] and items[index] < items[index + 1]:
            middles.append(index)
    return middles

find_middle_positions(numbers)

🧑🏽‍🎨 Sorted Sequence¶

Let the term sorted sequence refer to a series of numbers in consecutive positions in a list that exhibit sorted order.

E.g. 1, 5, 8 are in sorted order.

Given a list of numbers, write a function that takes a list of numbers and an integer size and returns True if the list contains a sorted sequence of length size.

sorted_sequence.py¶

NOTES

  • Draw it out
    • what are we looking for?
    • what is the overall strategy?
  • Boundary conditions
    • where are the boundaries? how do we deal with them?
In [ ]:
def is_sorted_sequence(items, index, size):
    """Returns True if the list contains a sorted sequence of length `size` starting at position `index`."""
    for i in range(index + 1, index + size):
        if items[i] < items[i - 1]:
            return False
    return True


def has_sorted_sequence(items, size):
    """Returns True if the list contains a sorted sequence of length `size`."""
    for index in range(len(items) + 1 - size):
        if is_sorted_sequence(items, index, size):
            return True
    return False
    

numbers = [1, 2, 5, 4, 8, 3, 6, 7, 9, 1]
for size in range(1, 7):
    print(size, has_sorted_sequence(numbers, size))

Now change the code to return the first position of a sorted sequence of length size.

Now change the code to return all positions of a sorted sequence of length size.

🧑🏼‍🎨 Roll Over¶

There were ten in a bed
And the little one said
"Roll over, roll over"
So they all rolled over
And one fell out

One at a time, remove and print the first name in the list and shift the remaining names to the left. Stop when only one name is left.

Print the list after each iteration.

Fill empty spaces with None.

roll_over.py¶

NOTES

  • Draw it out
    • write out the names
  • How do we print out the first value?
    • print(bed_occupants[0])
  • How do we shift everyone to the left?
    • draw out the idea - arrows pointing to new values
    • index and index + 1
    • what happens if we use range(len(bed_occupants))? => IndexError
  • How do we put a None at the end?
    • use bed_occupants[-1] = None
  • How many times do we iterate?
    • how many people do we start with?
    • how many people do we end with?
  • wrap the shifting loop in the outer loop
In [ ]:
bed_occupants = ['Mary', 'May', 'Michelle', 'Michaela', 'Manuela']

print(bed_occupants)
print()

for iteration in range(len(bed_occupants)-1):
    # Print the name of the first person
    print(bed_occupants[0])
    
    # Shift everyone else left
    for index in range(len(bed_occupants)-1):
        bed_occupants[index] = bed_occupants[index + 1]
    
    # Put a None at the end
    bed_occupants[-1] = None
    
    # Print
    print(bed_occupants)
    print()

👩🏻‍🎨 Rotate¶

Given a list, shift every item to the right one space. Items on the end wrap around to the front.

rotate.py¶

NOTES

  • Draw this out. What does it mean?
    • demonstrate how items on the right end get moved to the front of the list
In [ ]:
def rotate_right(items):
    """Shift the items one space to the right. Wrap the item on the end around to the front.
    Modifies the input list.
    """
    tmp = items[-1]
    for index in range(len(items)-1):
        items[len(items)-index-1] = items[len(items)-index-2]
    items[0] = tmp
    

pets = ['fish', 'dog', 'cat', 'gerbil', 'gecko', 'salamander', 'parakeet']
print(pets)
rotate_right(pets)
print(pets)