Make 100 passes over the cats.
On the first pass, visit every cat and remove its hat. ๐
On the second pass, visit every other cat and replace its hat. ๐บ
On the third pass, visit every third cat. If the cat is hatted, remove its hat ๐ฟ. If the cat is un-hatted, replace its hat. ๐บ
On the 4th-100th passes, do the same. Add hats if they donโt exist and remove hats if they do.
On the 100th pass, you should only visit the 100th cat (the last cat), removing or replacing its hat appropriately.
Write a program that outputs which cats have hats ๐บ and which cats do not ๐พ at the end of the 100th iteration.
Inย [4]:
module Cats
def self.cats_in_hats(sequence)
count = sequence.length
count.times do |index|
(index..count).step(index + 1) do |n|
sequence[n] = !sequence[n]
end
end
sequence
end
end
go = Array.new(100, true)
Cats.cats_in_hats(go).select.with_index do |cat, i|
puts "Cat ##{i + 1} has no hat" unless cat
end
Out[4]: