代码之家  ›  专栏  ›  技术社区  ›  Teja Kantamneni

Ruby每个逻辑问题

  •  6
  • Teja Kantamneni  · 技术社区  · 14 年前

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

    打印数组的内容 十六位数,四位数 时间,使用 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}

    8 回复  |  直到 11 年前
        1
  •  6
  •   Andrew Grimm atk    14 年前

    i = 0
    a.each do |item|
      p a[i, 4] if(i % 4 == 0)
      i +=1
    end
    
        2
  •  2
  •   Markus Wahl    11 年前

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

    >> 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
    
        3
  •  1
  •   glenn mcdonald    14 年前

    x = 4
    (0...(a.size/x.to_f).ceil).each {|i| p a.slice(x*i,x)}
    
        4
  •  0
  •   Jamie Wong    14 年前

    (1..16).each do |item|
      print "#{item} "
      print "\n" if item % 4 == 0
    end
    
        5
  •  0
  •   beach    14 年前

    ##########
    # 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
    
        6
  •  0
  •   mrampton    14 年前

    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
    
        7
  •  0
  •   Andrew Grimm atk    14 年前

    each_with_index

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

    i % 4 == 0

        8
  •  0
  •   cdated    13 年前

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