diff --git a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py index 74cb1ed..5239634 100644 --- a/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py +++ b/ch09-lists-tuples-and-dictionaries/9a-challenge-cats-with-hats.py @@ -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)
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: