开发者

Ruby each logic question

I am trying to solve a simple Ruby problem from Seven Languages in Seven Weeks

Print the contents of an array of sixteen numbe开发者_如何学Gors, four numbers at a time, using just each

Here is what I came up with, can this be done in a simple way or make it better??

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

It can done using each_slice in one line

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


Teja, your solution is ok. As you need to use each, the algorithm complexity is going to be bounded to the size of your array.

I came up with the solution bellow. It is the same idea of yours except that it does not use an aux var (j) to store partial results.

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


Glenn Macdonald's is short, but it uses slice which is not allowed (only each, remember). Here is mine:

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

which also works well for other array sizes, here applied to an 18 sized array:

>> 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


I think this ought to work for any size array and any chunk-size x:

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


Try this:

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


The problem did not state that the array of sixteen numbers were sequential or started at one... let's created a solution that works for any 16 numbers.

##########
# 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


I used something similar to Miguel's, though his is cleaner:

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


Are you forbidden from using each_with_index? If not, building on @Miguel's answer:

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

I also replaced i % 4 == 0 with something that sounds like English ("i modulo 4 is zero")


A one-liner without slice:

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜