Arrays 如何在ruby中使用while而不使用'each''map'或'collect'方法对数字数组进行平方运算?

Arrays 如何在ruby中使用while而不使用'each''map'或'collect'方法对数字数组进行平方运算?,arrays,ruby,methods,square,Arrays,Ruby,Methods,Square,我不熟悉用RUBY编码。我试图写一个方法,将一个数字数组中的每个元素平方,然后返回一个新的平方数组。尝试使用while循环而不使用每个、收集或映射。无法理解如何索引/循环数组和平方的每个元素(**) 这对我来说是有意义的,但我知道这是错误的 def square_array(numbers) count = 0 while count < numbers.length do numbers.index ** 2 end square_array(numbers)

我不熟悉用RUBY编码。我试图写一个方法,将一个数字数组中的每个元素平方,然后返回一个新的平方数组。尝试使用while循环而不使用
每个
收集
映射
。无法理解如何索引/循环数组和平方的每个元素(**)

这对我来说是有意义的,但我知道这是错误的

def square_array(numbers)
  count = 0
  while count < numbers.length do
    numbers.index ** 2 
  end
  square_array(numbers)
end 
def方形阵列(数字)
计数=0
而count

有人能帮我吗?谢谢

简单的方法是
map
,当然:

def square_array(numbers)
    numbers.map { |e| e ** 2 }
end 
但是下面是对
while
循环(这是一个很好的实践)执行相同操作所必须做的事情

  • 创建一个数组以包含转换后的数据
  • 创建一个计数器(您已经这样做了)
  • 设置
    while
    循环(按您现有的设置,除非您不需要在最后使用
    do
  • 编写一条语句,将索引与计数器相同的数组元素平方,并将结果推送到步骤1中创建的数组中
  • 将计数器增加1(您忘记了这样做,因此将得到一个无止境的循环,因为
    count
    始终等于零)
  • 返回在步骤1中创建的数组
  • 那就够了!看看你能不能把它们放在一起,而不是我只给你代码。

    def square\u数组(数字)
    
    def square_array(numbers)
      # Allocate an array with the same size as `numbers`
      # so that the runtime does not have to resize it from time to time
      result = Array.new(numbers.size)
    
      # The index
      i = 0
    
      while i < numbers.size
        # Fill the result array
        result[i] = numbers[i] ** 2
    
        # and don't forget to increase the index,
        # otherwise the loop will run forever.
        i += 1
      end
    
      # Return the result array
      result
    end
    
    #分配一个与'numbers'大小相同的数组` #这样运行时就不必不时调整其大小 结果=数组.new(number.size) #索引 i=0 而我<数字 #填充结果数组 结果[i]=数字[i]**2 #别忘了增加指数, #否则,循环将永远运行。 i+=1 结束 #返回结果数组 结果 结束
    更实用的方法是使用递归

    fun =
      ->(acc = [], arr, map, fun) {
        arr.empty? ? acc : fun.(acc << map.(arr.shift), arr, map, fun)
      }
    #⇒ #<Proc:0x000055ab64333fa0@(pry):12 (lambda)>
    
    请注意!这种方法会改变初始数组,因此在传递给函数之前应该显式地
    array.dup
    。为了消除传递函数本身并保持初始数组不变的必要性,我们需要一个包装器

    fun =
      ->(acc = [], arr, map, fun) {
        arr.empty? ? acc : fun.(acc << map.(arr.shift), arr, map, fun)
      }
    #⇒ #<Proc:0x000055ab64333fa0@(pry):12 (lambda)>
    mapper = ->(arr, map) { fun.([], arr.dup, map, fun) }
    
    看,还有。可以使用for循环来代替
    数组#每个

    ary = [1,2,3]
    
    res = []
    for n in ary do
      res << n ** 2
    end
    
    res
    #=> [1, 4, 9]
    
    ari=[1,2,3]
    res=[]
    对于n在arido中
    决议[1,4,9]
    
    但最好还是坚持使用
    map

    以下是我的解决方案:

    def square_array(numbers)
      new_array = []
      counter = 0 
      while counter < numbers.length()
      new_array.push(numbers[counter] * numbers[counter])
      counter += 1
      end 
      return new_array
    end
    
    def方形阵列(数字)
    新的_数组=[]
    计数器=0
    而计数器
    不使用each、map或collect

    def方形阵列(阵列)
    新的_数组=[]
    array.length.times do |索引|
    新建_array.push(数组[索引]**2)
    结束
    新阵列
    结束
    
    我最好先用
    然后用数字。以后再换。与
    size
    不同,这种方法适用于无限输入枚举数。但是这会改变参数。@AlekseiMatiushkin我认为保持参数
    numbers
    不变是返回新数组的关键,因此我不会使用
    shift
    。此外,如果
    numbers
    是一个无限枚举数,那么
    square\u array
    将永远不会返回,在这种情况下,最好
    产生
    一些东西。是的,同意<第一行中的code>numbers.dup
    有助于避免变异,但它会破坏惰性枚举,所以我真的不知道。它看起来像FORTRAN,但我认为它最接近OP想要的。@BobRodes根据我的基准测试(1000万个整数的平方),预分配比不预分配快约18%(0.641s vs 0.781s)。此外,
    Array#map
    比朴素的while循环(无预分配)快约9%(0.720s)。顺便说一句,
    Array#map
    也会在引擎盖下预先分配目标阵列。如果您能解释一下,您到底不清楚什么是有帮助的。这样,Ruby开发人员就可以改进文档,这样将来的开发人员就不会遇到同样的问题。帮助让世界变得更美好@JörgWMittag祝你好运。:)如果它改变了原始数组,它如何“更有效”?副作用与功能性几乎相反@JörgWMittag通过将此函数包装在
    ->(arr,map){fun([],arr.dup,map,fun)}
    中,我可以消除将
    fun
    作为一个名称传递的必要性和副作用,并在后脚本中明确说明。我决定不这样做是为了缩短代码。好的,我会的。这是非常有趣的东西,尽管我无法想象OP会知道你在干什么!不过,我相信它对其他人会有用。有趣的方法是,使用外部迭代器和
    循环
    ,这样就不必递增和跟踪计数器。
    
    def sq(arr)
      enum = arr.each
      a = []
      loop do
        n = enum.next
        a << n*n
      end
      a
    end
    
    sq [1, 2, 3, 4]
      #=> [1, 4, 9, 16]
    
    ary = [1,2,3]
    
    res = []
    for n in ary do
      res << n ** 2
    end
    
    res
    #=> [1, 4, 9]
    
    def square_array(numbers)
      new_array = []
      counter = 0 
      while counter < numbers.length()
      new_array.push(numbers[counter] * numbers[counter])
      counter += 1
      end 
      return new_array
    end