Lets say that I have a number of "things", for instance 7
.
假设我有很多“东西”,例如7。
But I can store this "things" in groups of maximum "2" units. So I would need to do something like this:
但是我可以将这些“东西”以最大的“2”为单位存储。所以我需要这样做:
7 ----> [2, 2, 2, 1]
The obvious method if to simply make a loop through it
最明显的方法是通过它做一个循环
def decompose(qty, group_max)
ret = []
while qty > 0
if qty < group_max
ret.push qty
qty = 0
else
ret.push group_max
qty -= group_max
end
end
ret
end
decompose 7, 2
While this works... it is not really ellegant. I was wondering if maybe there is a method in the integer or the array structure that I can use to improve this code.
虽然这工作……它并不是真正的精灵。我想知道在整数或数组结构中是否有一个方法可以用来改进这段代码。
I find cleaner to do things like
我发现更干净的东西
myarray.map {|x| ... }
and I was wondering if there was something similar that might help me with this.
我想知道是否有类似的东西可以帮助我。
7 个解决方案
#1
3
You can do this as:
你可以这样做:
qty = 15 # say
group_size = 2
count_of_groups = qty / group_size
result = [group_size] * count_of_groups
remaining = qty % group_size
result += [remaining] if remaining != 0
result # [2, 2, 2, 2, 2, 2, 2, 1]
#2
3
def decompose(qty, group_max)
q, r = qty.divmod(group_max)
(Array.new(q) { group_max }) + (r > 0 ? [r] : [])
end
#3
3
divmod
anyone?
divmod有人知道吗?
qty = 15 # say
group_size = 2
d, r = qty.divmod(group_size)
Array.new(d, group_size) << r # => [2, 2, 2, 2, 2, 2, 2, 1]
#4
2
I'd take advantage of the Array constructor: first paramter the number of elements, second parameter their value.
我将利用数组构造函数:第一个参数表示元素的数量,第二个参数表示元素的值。
def decompose(qty, group_max)
result = Array.new(qty / group_max, group_max)
remainder = qty % group_max
remainder == 0 ? result : result.push(remainder)
end
decompose(7, 2)
=> [2, 2, 2, 1]
A one line solution
一行的解决方案
def decompose(qty, group_max)
(Array.new(qty / group_max, group_max) + [qty % group_max]).reject(&:zero?)
end
#5
2
val, max = 8, 3
([max] * (val / max)).tap do |arr|
arr << val % max unless (val % max).zero?
end
#⇒ [3, 3, 2]
val, max = 7, 2
([max] * (val / max)).tap do |arr|
arr << val % max unless (val % max).zero?
end
#⇒ [2, 2, 2, 1]
or even:
甚至:
([max] * (val / max) + [val % max]).reject &:zero?
#6
1
Another way to skin the cat:
另一种剥猫皮的方法:
Lets say that I have a number of "things", for instance 7.
假设我有很多“东西”,例如7。
Let's use an array to represent that, each nil
is a "thing":
我们用一个数组来表示,每个nil都是一个"东西"
Array.new(7)
#=> [nil, nil, nil, nil, nil, nil, nil]
But I can store this "things" in groups of maximum "2" units:
但我可以将这些“东西”以最大的“2”为单位进行分组:
each_slice
can do that:
each_slice可以这样做:
Array.new(7).each_slice(2).to_a
#=> [[nil, nil], [nil, nil], [nil, nil], [nil]]
To get the number of "things" in each group:
获取每组“事物”的数量:
Array.new(7).each_slice(2).map(&:length)
#=> [2, 2, 2, 1]
#7
0
def decompose(n, grp_size)
nbr_groups, remainder = n.divmod(grp_size)
[grp_size]*nbr_groups << remainder
end
decompose(23, 3)
#=> [3, 3, 3, 3, 3, 3, 3, 2]
#1
3
You can do this as:
你可以这样做:
qty = 15 # say
group_size = 2
count_of_groups = qty / group_size
result = [group_size] * count_of_groups
remaining = qty % group_size
result += [remaining] if remaining != 0
result # [2, 2, 2, 2, 2, 2, 2, 1]
#2
3
def decompose(qty, group_max)
q, r = qty.divmod(group_max)
(Array.new(q) { group_max }) + (r > 0 ? [r] : [])
end
#3
3
divmod
anyone?
divmod有人知道吗?
qty = 15 # say
group_size = 2
d, r = qty.divmod(group_size)
Array.new(d, group_size) << r # => [2, 2, 2, 2, 2, 2, 2, 1]
#4
2
I'd take advantage of the Array constructor: first paramter the number of elements, second parameter their value.
我将利用数组构造函数:第一个参数表示元素的数量,第二个参数表示元素的值。
def decompose(qty, group_max)
result = Array.new(qty / group_max, group_max)
remainder = qty % group_max
remainder == 0 ? result : result.push(remainder)
end
decompose(7, 2)
=> [2, 2, 2, 1]
A one line solution
一行的解决方案
def decompose(qty, group_max)
(Array.new(qty / group_max, group_max) + [qty % group_max]).reject(&:zero?)
end
#5
2
val, max = 8, 3
([max] * (val / max)).tap do |arr|
arr << val % max unless (val % max).zero?
end
#⇒ [3, 3, 2]
val, max = 7, 2
([max] * (val / max)).tap do |arr|
arr << val % max unless (val % max).zero?
end
#⇒ [2, 2, 2, 1]
or even:
甚至:
([max] * (val / max) + [val % max]).reject &:zero?
#6
1
Another way to skin the cat:
另一种剥猫皮的方法:
Lets say that I have a number of "things", for instance 7.
假设我有很多“东西”,例如7。
Let's use an array to represent that, each nil
is a "thing":
我们用一个数组来表示,每个nil都是一个"东西"
Array.new(7)
#=> [nil, nil, nil, nil, nil, nil, nil]
But I can store this "things" in groups of maximum "2" units:
但我可以将这些“东西”以最大的“2”为单位进行分组:
each_slice
can do that:
each_slice可以这样做:
Array.new(7).each_slice(2).to_a
#=> [[nil, nil], [nil, nil], [nil, nil], [nil]]
To get the number of "things" in each group:
获取每组“事物”的数量:
Array.new(7).each_slice(2).map(&:length)
#=> [2, 2, 2, 1]
#7
0
def decompose(n, grp_size)
nbr_groups, remainder = n.divmod(grp_size)
[grp_size]*nbr_groups << remainder
end
decompose(23, 3)
#=> [3, 3, 3, 3, 3, 3, 3, 2]