【问题标题】:Placement of variable declaration in Ruby (beginner)Ruby 中变量声明的放置(初学者)
【发布时间】:2016-09-24 20:28:41
【问题描述】:

我正在通过一些在线练习问题来学习 Ruby,虽然我能够解决这个问题,但我很难理解关于变量声明的位置的一些事情。

为什么当我在 idx2 while 循环内声明/定义“对”(数组索引的总和)时,以下代码有效,但在声明索引本身后直接这样做时无效?

def two_sum(nums)
  idx=0
  idx2=0
  while idx<nums.length-1
    idx2=idx+1 
    while idx2<nums.length
      pair=nums[idx]+nums[idx2]
      if pair==0
        return [idx, idx2] 
      else
        idx2+=1 
      end 
    end 
    idx+=1 
  end 
end

上面的版本可以,但是下面的结构不行。

def two_sum(nums)
  idx=0
  idx2=0
  pair=nums[idx]+nums[idx2]
  while idx<nums.length-1
    idx2=idx+1 
    while idx2<nums.length
      if pair==0
        return [idx, idx2] 
      else
        idx2+=1 
      end 
    end 
    idx+=1 
  end 
end

如果有人可以对此提供解释或一些入门级资源,我将不胜感激。谢谢。

编辑:

对于所需输出的缩进和特异性问题,我们深表歉意。我对此完全陌生,有时细节会溜走,我感谢您的反馈。感谢您一步一步的逻辑运行,这正是我想要的。

【问题讨论】:

  • work 是什么意思?您预期的输入/输出值是多少?
  • 编辑了您的代码以修复缩进。在第二个示例中,pair 变量在while 的每次迭代中都将具有相同的值。首先它可以改变,因为idx2 可以增加。不错的用户名顺便说一句:)

标签: ruby variables syntax declaration


【解决方案1】:

让我们一次看一下工作代码和损坏的代码。

假设nums = [1, 2, -1, 3]

可能工作的代码

 1 def two_sum(nums)
 2   idx  = 0
 3   idx2 = 0
 4
 5   while idx < nums.length-1
 6     idx2 = idx+1 
 7     while idx2 < nums.length
 8       pair = nums[idx] + nums[idx2]
 9       if pair == 0
10          return [idx, idx2] 
11       else
12         idx2 += 1 
13       end 
14     end 
15     idx += 1 
16   end 
17 end

什么时候开始

idx = 0, idx2 = 0

现在我们处于第一个 while 循环(第 6 行)

idx = 0, idx2 = 1

第二个while循环(第8行)

idx = 0, idx2 = 1, pair = nums[0] + nums[1] = 3

pair != 0,所以我们现在处于else 语句中。 (第 12 行)

idx = 0, idx2 = 2, pair is still 3

我们现在循环回到第二个 while 循环(再次是第 8 行)

idx = 0, idx2 = 2, pair = nums[0] + nums[2] = 0

pair == 0,所以我们返回 [0, 2](第 10 行)

这就是代码的运行方式。

破解密码

 1 def two_sum(nums)
 2   idx  = 0
 3   idx2 = 0
 4   pair = nums[idx] + nums[idx2]
 5   while idx < nums.length-1
 6     idx2 = idx+1 
 7     while idx2 < nums.length
 8       
 9       if pair == 0
10          return [idx, idx2] 
11       else
12         idx2 += 1 
13       end 
14     end 
15     idx += 1 
16   end 
17 end

我们将再次运行相同的逻辑。

在我们到达第一个 while 循环之前(第 2 - 4 行)

idx = 0, idx2 = 0, pair = nums[0] + nums[0] = 2

现在我们进入第一个 while 循环,(第 6 行)

idx = 0, idx2 = 1, pair = 2

现在在第二个 while 循环中,pair != 0,所以我们点击了else 语句,(第 12 行)

idx = 0, idx2 = 2, pair = 2

现在我们循环回到第二个 while 循环的开始。 pair != 2,所以我们点击了else 声明。

idx = 0, idx2 = 3, pair = 2

pair != 0,所以我们点击了else 声明。

idx = 0, idx2 = 4, pair = 2

所以现在,idx2 &lt; nums.lengthfalse, so we exit the second while loop, and nowidx = 1`,我们继续循环。

如您所见,因为您在 while 循环之外声明了 pair,所以它永远不会重新计算为不同的值,因此您永远不会遇到 if 语句,因为 pair 永远不会等于零,当然除非,您在初始总和上得到 0

while 循环适用于不确定需要多少循环的情况。一个例子是检查用户的输入,您可能需要循环一次或十次,具体取决于用户何时正确输入。在这种情况下,您确切知道需要运行多少次。所以,迭代器更适合这里的工作。

这是一个如何编写的示例。请记住,可能有更好的方法来做到这一点,我只是想展示一个例子。

def two_sum(nums)
  nums.each_with_index do |num1, idx1|
    nums.each_with_index do |num2, idx2|
      next if idx1 == idx2 
      return [idx1, idx2] if num1 + num2 == 0
    end
  end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多