Ruby 每个逻辑问题

发布于 2024-10-01 02:28:10 字数 484 浏览 6 评论 0原文

我正在尝试解决七周七种语言

打印数组的内容 十六个数字,四个数字在一个 时间,仅使用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| px}

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

Print the contents of an array of
sixteen numbers, 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}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

愁杀 2024-10-08 02:28:11

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

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

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

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
゛时过境迁 2024-10-08 02:28:11

格伦·麦克唐纳的很短,但它使用了不允许的切片(记住,只有每个切片)。这是我的:

(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

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
温馨耳语 2024-10-08 02:28:11

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

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

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)}
站稳脚跟 2024-10-08 02:28:11

试试这个:

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

Try this:

(1..16).each do |item|
  print "#{item} "
  print "\n" if item % 4 == 0
end
偏闹i 2024-10-08 02:28:11

该问题并未说明 16 个数字的数组是连续的或从 1 开始...让我们创建一个适用于任何 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

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
梅倚清风 2024-10-08 02:28:11

我使用了类似于 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

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
相权↑美人 2024-10-08 02:28:11

您是否被禁止使用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”)

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

妖妓 2024-10-08 02:28:11

没有切片的单行:

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

A one-liner without slice:

a.each {|i| p a[i,4] if (i+3) % 4 == 0}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文