Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# At the start all cats are hatless.
cats = [False]*101

# We do a loop over each step size from 1 to 100
for step_size in range(1, 100 + 1):

# For each step size we calculate how many cats we are going to visit
cats_to_visit = 100 // step_size

# We only loop over cat indices that are a multiple of our step size
for i in range(1, cats_to_visit + 1):

# We reverse the hat status for each cat we loop over
cats[i*step_size] = not cats[i*step_size]

# Print the list of cats that have a hat
for i in range(101):
if cats[i]:
print(f"Cat {i} has a hat!")