嵌套循环Ruby [英] Nested Loops Ruby

查看:119
本文介绍了嵌套循环Ruby的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

难以理解这个嵌套循环问题:

Having Difficulty understanding this nested loop problem:

你有10个鹅卵石(编号为1-10)。它们默认为黑色。如果它们是黑色的,则必须将它们涂成白色,或者如果它们是白色则将它们涂成黑色。共有10轮。每一轮,你必须改变当前轮次倍数的鹅卵石。
鹅卵石默认为黑色。


  • 第一轮,你改变每一块鹅卵石(将它们全部涂成白色) 。

  • 第二轮,你改变所有其他鹅卵石(你画鹅卵石
    #2,4,6,8,10黑色)。

  • 第3轮,你改变鹅卵石#3,6,9。

  • 第四轮你改变鹅卵石#4,8。

  • ...

  • ...

  • 第10轮,你改变鹅卵石#10。

  • 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.

第10轮后,鹅卵石是涂成黑色,涂成白色?

After the 10th round, which pebbles are painted black and which are painted white?

我的解决方案没有运行在下面(我尝试通过制作一组数字(转向字符串)和如果涂成白色则添加w,如果涂成黑色则添加w。

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:

element % round == 0

它应该是:

(index+1) % round

您想要比较鹅卵石的指数而不是鹅卵石的当前值。同样,您需要记住索引是从0开始的(即它们从0开始计数)。您需要将索引设置为1(因此添加1)否则第一个元素将始终更改而其他元素将关闭1.

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.

也是 pebble_color 的拼写错误,应该是 pebble_colors

There was also a typo for pebble_color, which should be pebble_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):

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 (index+1) % round == 0}
  pebbles_to_paint.each do |pebble_number|
    if pebble_colors[pebble_number].include?("w")
      pebble_colors[pebble_number].delete!("w")
    else
      pebble_colors[pebble_number] << "w"
    end
  end
  p pebble_colors
end

输出为:

["1w", "2w", "3w", "4w", "5w", "6w", "7w", "8w", "9w", "10w"]
["1w", "2", "3w", "4", "5w", "6", "7w", "8", "9w", "10"]
["1w", "2", "3", "4", "5w", "6w", "7w", "8", "9", "10"]
["1w", "2", "3", "4w", "5w", "6w", "7w", "8w", "9", "10"]
["1w", "2", "3", "4w", "5", "6w", "7w", "8w", "9", "10w"]
["1w", "2", "3", "4w", "5", "6", "7w", "8w", "9", "10w"]
["1w", "2", "3", "4w", "5", "6", "7", "8w", "9", "10w"]
["1w", "2", "3", "4w", "5", "6", "7", "8", "9", "10w"]
["1w", "2", "3", "4w", "5", "6", "7", "8", "9w", "10w"]
["1w", "2", "3", "4w", "5", "6", "7", "8", "9w", "10"]

这篇关于嵌套循环Ruby的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆