【问题标题】:Ruby each logic questionRuby每个逻辑问题
【发布时间】:2011-05-05 10:06:28
【问题描述】:

我正在尝试从Seven Languages in Seven Weeks 解决一个简单的 Ruby 问题

打印数组的内容 十六个数字,四个数字 时间,只使用each

这是我想出的,可以用简单的方式完成还是做得更好??

a = (1..16).to_a

i = 0
j = []
a.each do |item|
  i += 1 
  j << item
  if(i % 4 == 0)
    p j
    j = []
  end
end

可以在一行中使用each_slice 完成

a.each_slice(4){|x| p x}

【问题讨论】:

    标签: ruby arrays each


    【解决方案1】:

    Glenn Macdonald's 很短,但它使用了不允许的切片(记住,只有每个切片)。这是我的:

    (0...a.size).each {|index| p a[index, 4] if index % 4 == 0}
    

    这也适用于其他数组大小,这里适用于 18 大小的数组:

    >> a = (113..150).to_a.insert(5,55).insert(10,66666).shift(18)
    => [113, 114, 115, 116, 117, 55, 118, 119, 120, 121, 66666, 122, 123, 124, 125, 126, 127, 128]
    >> (0...a.size).each {|index| p a[index, 4] if index % 4 == 0}
    [113, 114, 115, 116]
    [117, 55, 118, 119]
    [120, 121, 66666, 122]
    [123, 124, 125, 126]
    [127, 128]
    => 0...18
    

    【讨论】:

      【解决方案2】:

      没有切片的单行:

      a.each {|i| p a[i,4] if (i+3) % 4 == 0}
      

      【讨论】:

        【解决方案3】:

        Teja,你的解决方案没问题。由于您需要使用每一个,算法复杂度将受限于数组的大小。

        我想出了下面的解决方案。除了不使用 aux var (j) 来存储部分结果之外,它与您的想法相同。

        i = 0
        a.each do |item|
          p a[i, 4] if(i % 4 == 0)
          i +=1
        end
        

        【讨论】:

        • 好一个。唯一不使用辅助变量的解决方案。谢谢。
        • 问题指定了each的使用,但没有说它必须用在哪个数组上!
        【解决方案4】:

        您是否被禁止使用each_with_index?如果没有,请以@Miguel 的回答为基础:

        a.each_with_index do |item, i|
          p a[i, 4] if i.modulo(4).zero?
        end
        

        我还将i % 4 == 0 替换为听起来像英语的东西(“i modulo 4 is zero”)

        【讨论】:

          【解决方案5】:

          我使用了类似于 Miguel 的东西,虽然他的更干净:

          array = (1..16).to_a
          i = 0
          array.each do
            puts array[i...i+4].to_s if i % 4 == 0 and i+4 <= array.size
            i+=4 
          end
          

          【讨论】:

            【解决方案6】:

            我认为这应该适用于任何大小的数组和任何块大小的 x:

            x = 4
            (0...(a.size/x.to_f).ceil).each {|i| p a.slice(x*i,x)}
            

            【讨论】:

              【解决方案7】:

              问题并没有说明十六个数字的数组是连续的还是从一个开始...让我们创建一个适用于任何 16 个数字的解决方案

              ##########
              # Method 1 - Store chunks of 4 and print at the end
              ##########
              a = (1..16).to_a
              b = []
              a.each do |item|
                  b << [] if b.size == 0
                  b << [] if b[-1].size == 4
                  b[-1] << item
              end
              
              # choose your desired printing method
              print b 
              b.each{|c| puts c.join(",")}
              
              ##########
              # Method 2 - print the chunks as they are encountered
              ##########
              
              # Note: "p" was specifically chosen over "print" because it returns the value printed instead of nil.
              #       If you use a different printing function, make sure it returns a value otherwise the 'b' array will not clear.
              
              
              # Note: This implementation only prints out all array entries if a multiple of 4
              #       If 'b' contains any items outside the loop, they will not be printed
              a = (1..16).to_a
              b = []
              a.each do |item|
                  b << item
                  b = [] if b.size == 4 and puts b
              end
              
              
              # Note: This implementation will print all array elements, even if number of elements is not multiple of 4.
              a = (1..16).to_a
              b = []
              a.each do |item|
                  b = [] if b.size == 4 and p b
                  b << item
              end
              p b
              

              【讨论】:

                【解决方案8】:

                试试这个:

                (1..16).each do |item|
                  print "#{item} "
                  print "\n" if item % 4 == 0
                end
                

                【讨论】:

                • 我想过这个问题,但问题是一次打印四个数字,这个解决方案打印一个数字并添加一个换行符,这可能与他们要求的解决方案不同
                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2011-04-16
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                相关资源
                最近更新 更多