Having Difficulty understanding this nested loop problem:
You have 10 pebbles (numbered 1-10). They are by default black. You must alter them by painting them white if they are black or painting them black if they are white. There are 10 rounds. Every round, you must alter the pebbles that are multiples of the current round. The pebbles are by default black.
- 1st round, you alter every pebble (paint them all white).
- 2nd round, you alter every other pebble(you paint pebbles #2,4,6,8,10 black).
- 3rd round, you alter pebbles #3,6,9.
- 4th round you alter pebbles #4,8.
- ...
- ...
- 10th round, you alter pebble #10.
After the 10th round, which pebbles are painted black and which are painted white?
My solution which doesn't run is below (I attempt to do so by making an array of numbers(turned to strings) and adding "w" if painted white and deleting "w" if painted black.
(I have tried editing it to make it run, however I am new to nested loops and I am just not grasping this concept). I would greatly appreciate it if someone could explain to me what I am doing wrong and give a better solution.
pebbles = (1..10).map {|element| element.to_s}
pebble_colors = (1..10).map {|element| element.to_s}
(1..10).each do |round|
pebbles_to_paint = []
pebbles.each_with_index {|element, index| pebbles_to_paint << index if element % round == 0}
pebbles_to_paint.each do |pebble_number|
if pebble_color[pebble_number].include?("w")
pebble_color[pebble_number].delete!("w")
else
pebble_color[pebble_number] << "w"
end
end
end
Your main problem appears to be in the deciding of which pebbles to paint. The following is not right:
It should be:
You want to compare the pebble's index rather than the current value of the pebble. As well, you need to remember that indexes are 0-based (ie they start counting from 0). You need to have the indexes be 1-based (hence the adding of 1) otherwise the first element would always change and the others would be off by 1.
There was also a typo for
pebble_color
, which should bepebble_colors
.You could definitely re-factor the code to make it shorter, but the following appears to work (just making the minimal changes mentioned above):
The output is:
For make the problem simple, I think it is better calculate all round multiples before update the pebble.
Because this is about nested loops, I just wanted to add that you don't necessarily have to iterate through all pebbles on each round. (that's 100 iterations for 10 pebbles!)
Instead you can use
Range#step
to iterate over each nth element, starting with the round's index:produces:
That's only 27 iterations. A nice side effect is that you don't have to calculate the remainder any more.
Full example:
There are a couple of problems in your code.
You use
element % round
instead of(index + 1) % round
You modify array
pebble_color
instead ofpebble_colors
Fixing these two problems, the program leaves
pebble_colors
with the valuealthough I think that isn't quite what you had in mind!
You have three arrays where one will do. All you need is an array of ten colours, starting all black. I would code it like this
output