I am stuck on this code problem - I need to iterate through an array of booleans 100 times. Instructions:
Cats in hats
You have 100 cats. Your rules are simple: whenever you visit a cat, you toggle it's hat status (if it already has a hat, you remove it.. if it does not have a hat, you put one on). All of the cats start hat-less. You cycle through 100 rounds of visiting cats. In the 1st round, you visit every cat. In the second round, you visit every other cat. In the nth round, you visit every nth cat.. until the 100th round, in which you only visit the 100th cat. At the end of 100 rounds, which cats have hats?
New to Ruby from JS and stuck on how to implement this. I think my logic is right, I just can't get it to change the value of the booleans in the array.
Any advice much appreciated!
Thanks
def cats_in_hats
cats = []
i = 0
while i< 100
cats << false
i = i + 1
end
puts cats
round = 1;
while round < 10
#
cats = cats.each_with_index do |cat, idx|
if idx % round == 0
puts "toggle index #{idx} divisible by round #{round}"
cat = !cat
puts cat
end
end
round = round + 1
end
puts cats
end
puts "------Cats in Hats------"
puts cats_in_hats == [1, 4, 9, 16, 25, 36, 49, 64, 81, 100]