Closed ghost closed 7 years ago
I also just figured that out there is my version of the code :P using list options ;)
haveHat = []
counter = 1
while counter <= 101: for i in range(1, 101): if i % counter == 0: if i in haveHat : haveHat.remove(i) else: haveHat.append(i) counter += 1
print (haveHat)
Here's my solution. I set up a dict of booleans, ran 100 rounds, then printed the keys that were still True
at the end.
# Initially cats 1-100 do not have a hat
cat_hats = {k: False for k in range(1, 101)}
# For every n-th cat: if has hat take it off, if no hat put it on
for n in range(1, 101):
for cat in cat_hats.keys():
if cat % n == 0:
cat_hats[cat] = not (cat_hats[cat])
# Output which cats have hats when you're done
for cat,hat in cat_hats.items():
if hat:
print('Cat # {} has a hat!'.format(cat))
Updated in v 2.0 of the courses. Coming week of 12/19/2016. https://github.com/realpython/about/blob/master/changelog.csv
cats={} for i in range(1,101): cats[i]=0
for round in range(1,101): # 100 rounds for cat in cats: # 100 cats if cat % round == 0: cats[cat] = abs(cats[cat]-1) # 1 = hat, 0 = no hat
for i in cats: if cats[i]==1: print "Cat with hat: {}".format(i)