Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update 9a-challenge-cats-with-hats.py #86

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
54 changes: 24 additions & 30 deletions ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,27 @@
# 9.9 - Challenge: Cats With Hats
# Solution to challenge


def get_cats_with_hats(array_of_cats):
cats_with_hats_on = []
# We want to walk around the circle 100 times
for num in range(1, 100 + 1):
# Each time we walk around, we visit 100 cats
for cat in range(1, 100 + 1):
# Determine whether to visit the cat
# Use modulo operator to visit every 2nd, 3rd, 4th,... etc.
if cat % num == 0:
# Remove or add hat depending on
# whether the cat already has one
if array_of_cats[cat] is True:
array_of_cats[cat] = False
ct_hats = []
numberoftrue = 0
def determinect_hats():
# create a list of 100 false values in before round 1 which shows no hat on cat
catswithhat = [False]*100
# now start from round 1
for rounds in range(1,101):
#start with cat 1
for cats in range(1,101):
# logic as per the rounds
if (cats%rounds ==0):
# cats-1 because catswithhat has index from 0 to 99 which counts to 100 values
# the for loop for cats end with 100 so cats-1
if catswithhat[cats-1] == True:
catswithhat[cats-1] = False
else:
array_of_cats[cat] = True

# Add all number of each cat with a hat to list
for cat in range(1, 100 + 1):
if array_of_cats[cat] is True:
cats_with_hats_on.append(cat)

# Return the resulting list
return cats_with_hats_on
catswithhat[cats-1] = True
# tracking the indices using below code and storing the index value only if value is true
true_indexes = [index for index, value in enumerate(catswithhat) if value]
return (catswithhat.count(True)),(true_indexes)

numberoftrue,index_list = determinect_hats()
print(numberoftrue)

# Cats contains whether each cat already has a hat on,
# by default all are set to false since none have been visited
cats = [False] * (100 + 1)
print(get_cats_with_hats(cats))
#index+1 should be added because index starts from 0
new_index_list = [x+1 for x in index_list]
print(new_index_list)