You have a list of names. Write a function that returns the index of 'Waldo'. Return None
if Waldo is not present.
waldo.py
¶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'])
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.
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
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)
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
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
.
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
print(bed_occupants[0])
range(len(bed_occupants))
? => IndexError
None
at the end?bed_occupants[-1] = None
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()
Given a list, shift every item to the right one space. Items on the end wrap around to the front.
rotate.py
¶NOTES
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)