【问题标题】:MULTIPLYING an array ELEMENT in while loop ruby在while循环ruby中乘以数组元素
【发布时间】:2019-04-13 17:04:54
【问题描述】:

我有一个整数数组,我试图在数组中向后移动每个第二个元素乘以 2。但是,它似乎不接受数组项上的 * 运算符,导致错误消息:undefined method `*' for nil:NilClass (NoMethodError)

 index = array.length
  while index > 0
    array[index] * 2
    index - 2
  end

我检查了数组项的类,它确实是一个整数,所以我不确定为什么这不起作用。

谢谢。

【问题讨论】:

  • 你没有对array[index] * 2 和index - 2 表达式的值做任何事情,你需要将它们分配给一些东西:array[index] *= 2,array[index] = array[index] * 2,index = index - 2,@987654327 @, ...
  • 试过这样做,仍然导致“未定义的方法 `*' for nil:NilClass (NoMethodError)”
  • 它是一个整数数组:[4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]跨度>
  • 你的索引应该是array.length - 1(记住,数组是0索引的)。你也没有对array[index] * 2 做任何事情(要么打印它,要么分配给一个变量)
  • 输入数组为[4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2],您期望输出是什么?我将“每隔一个元素”解释为结果应该是[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]。对吗?

标签: ruby loops class operators


【解决方案1】:

如果您按照其他人的建议修复索引,并更改第三和第四行以实际修改数组和索引变量,那么您的代码对于任何具有偶数个元素的数组都可以正常工作。

index = array.length - 1
while index > 0
  array[index] *= 2
  index -= 2
end

# array now =='s [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

或者,您可以使用map.with_index 在一行中执行操作,因为数组的每个第二个元素都有一个奇数索引。

array.map.with_index { |n, i| i.odd? ? n * 2 : n }
# Or if you prefer,
array.map.with_index { |n, i| if i.odd? then n * 2 else n end }

#=> [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]

【讨论】:

    【解决方案2】:

    您可以使用索引迭代反转数组,然后反转结果Array#reverse i % 3 用于将每个第三个元素相乘。

    在您的示例中出现错误undefined method *' for nil:NilClass (NoMethodError),因为array.length 是16(数组中的元素),但索引从0 开始,所以最后一个索引是15,并且在第一次迭代中,您试图访问数组[16]不存在,所以是nil

    array = [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]
    result = array.reverse.each_with_index.map do |x, i|
      (i % 3).zero? ? x * 2 : x
    end.reverse
    # p result
    #=> [8, 2, 4, 4, 4, 2, 8, 2, 4, 4, 4, 2, 8, 2, 4, 4]
    

    更新:Sekalf Nroc 的答案很好 odd? 如果您需要每个第二个元素相乘,但如果您有例如 15 个元素,您可能会看到意想不到的结果,所以 reverse 仍然需要遍历数组反过来,通过结合这些方法,您可以执行以下操作:

    array = [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 99]
    p array.map.with_index { |n, i| i.odd? ? n * 2 : n } # WRONG RESULT
    #=>[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 99]
    p array.reverse.map.with_index { |n, i| i.even? ? n * 2 : n }.reverse
    #=>[8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 8, 2, 198]
    

    【讨论】:

      【解决方案3】:

      另一种方法,使用Numeric#step。 它适用于所有示例,即使是另一个步骤(array3)

      array1 = [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]
      array2 = [2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]
      array3 = [2, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 2, 4, 4, 2]
      
      (array1.size - 1).step(0, -2).each { |i| array1[i] *= 2 }
      (array2.size - 1).step(0, -2).each { |i| array2[i] *= 2 }
      (array3.size - 1).step(0, -3).each { |i| array3[i] *= 2 }
      
      p array1, array2, array3
      #=>[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
      #=>[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
      #=>[4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
      

      【讨论】:

        【解决方案4】:

        您可以使用提供Range#step 方法的范围。例如,给定数组:

        ary = [4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]
        

        这是一种使用Array#reverse_each“将数组中的每个第二个元素乘以 2”的方法:

        (1...ary.size).step(2).each { |i| ary.size.odd? ? ary[i] *=2 : ary[i-1] *=2 }
        #=> [4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4]
        

        请注意,第一部分 (1...ary.size).step(2).to_a 返回奇数索引:

        #=> [1, 3, 5, 7, 9, 11, 13, 15]
        

        还要注意范围内的三个点...。

        对于ary = [2, 4, 2] #=> [2, 8, 2]

        对于ary = [2, 4, 2, 4] #=> [4, 4, 4, 4]

        【讨论】:

        • 不错的方法,但它不是一个通用的解决方案,试试[2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2, 4, 2]
        • @YuriyVerbitskiy,谢谢。已修复,我误解了 OP。即使我还有一个疑问。
        • 我尝试了 Numeric#step 并添加了一个新答案,在这种情况下使用它看起来更好
        猜你喜欢
        • 1970-01-01
        • 2018-08-07
        • 1970-01-01
        • 1970-01-01
        • 2016-09-18
        • 1970-01-01
        • 2019-09-14
        • 1970-01-01
        • 2017-10-28
        相关资源
        最近更新 更多