Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

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

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
Ranjit9632 wants to merge1 commit intorealpython:master
base:master
Choose a base branch
Loading
fromRanjit9632:patch-3
Open
Changes fromall 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
View file
Open in desktop
Original file line numberDiff line numberDiff 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)

[8]ページ先頭

©2009-2025 Movatter.jp