将数组填充为特定大小

时间:2022-09-20 21:28:54

There's probably a more efficient and more Ruby-ish way to do this:

也许有一种更有效、更像橡胶的方法可以做到这一点:

# Pad array to size n by adding x's. Don't do anything if n <= a.length.
def padleft(a, n, x)
  return a if n <= a.length
  return padleft([x] + a, n, x)
end

What would you suggest?

你建议什么?

7 个解决方案

#1


23  

Edited due to my misunderstanding of the question. Pervious version of my answer padded from the right side, but the question was asking to do it from the left side. I corrected it accordingly. This is due to naming convention. ljust, rjust are builtin methods for String, and I extended that convention to Array, but that corresponds to padright and padleft, respectively, in the terminology of the question.

由于我对这个问题的误解而编辑。我的回答的前一个版本是从右边填充的,但问题是要从左边填充。我纠正它。这是由于命名约定。ljust, rjust是字符串的内建方法,我将这个约定扩展到数组,但是在问题的术语中,它分别对应于padright和padleft。

Destructive methods

破坏性的方法

def padleft!(a, n, x)
  a.insert(0, *Array.new([0, n-a.length].max, x))
end
def padright!(a, n, x)
  a.fill(x, a.length...n)
end

It would be more natural to have it defined on Array class:

在数组类中定义它会更自然:

class Array
  def rjust!(n, x); insert(0, *Array.new([0, n-length].max, x)) end
  def ljust!(n, x); fill(x, length...n) end
end

Non-destructive methods

非破坏性的方法

def padleft(a, n, x)
  Array.new([0, n-a.length].max, x)+a
end
def padright(a, n, x)
  a.dup.fill(x, a.length...n)
end

or

class Array
  def rjust(n, x); Array.new([0, n-length].max, x)+self end
  def ljust(n, x); dup.fill(x, length...n) end
end

#2


4  

Using 10 for the length to pad to, and 'x' to be what you're padding to, this pads right:

用10表示要填充的长度,x表示要填充的长度,这个垫子是对的:

>> asdf = %w[0 1 2 3 ] #=> ["0", "1", "2", "3"]
>> asdf += (asdf.size < 10) ? ['x'] * (10 - asdf.size) : [] #=> ["0", "1", "2", "3", "x", "x", "x", "x", "x", "x"]

or

>> asdf = (asdf.size < 10) ? ['x'] * (10 - asdf.size) + asdf : asdf #=> ["x", "x", "x", "x", "x", "x", "0", "1", "2", "3"]

to padleft

对padleft

If it makes sense to you to monkey-patch Array:

如果你觉得monkey-patch数组有意义:

class Array
  def pad_right(s, char=nil)
    self + [char] * (s - size) if (size < s)
  end

  def pad_left(s, char=nil)
    (size < s) ? [char] * (s - size) + self : self
  end
end

%w[1 2 3].pad_right(5, 'x') # => ["1", "2", "3", "x", "x"]
%w[1 2 3].pad_left(5, 'x') # => ["x", "x", "1", "2", "3"]

#3


4  

FWIW:

就其价值而言:

def rpad(item, padding, num)
  Array(item).fill padding, Array(item).size, num
end
# rpad "initialize value(s)", 0, 3
# => ["initialize value(s)", 0, 0, 0]

#4


2  

Using the * operator to repeat a list.

使用*操作符重复一个列表。

# Pad array to size n by adding x's. Don't do anything if n <= a.length.
def padleft(a, n, x)
  return a if n <= a.length
  return [x] * (n - a.length) + a
end

#5


1  

Perhaps more Rubyish ;)

也许更Rubyish;)

# Pad array to size n by adding x's. Don't do anything if n <= a.length.
def padleft(a, n, x)
  (n - a.size).times.inject(a) do |array, i|
    array << x
  end
end

#6


1  

If you're using Rails and want the padding on the right:

如果你正在使用Rails并且想要右边的填充:

[1,2,3,4,5,6].in_groups_of(4)
=> [[1, 2, 3, 4], [5, 6, nil, nil]]

This doesn't come anywhere near answering the question but it's what I ended up needing after visiting this page. Hope it helps someone.

这几乎不能回答这个问题,但这正是我在浏览完这一页后所需要的。希望它能帮助一些人。

#7


1  

Here's another fun one-liner to do it:

这是另一个有趣的俏皮话:

(non-destructive)

(无损)

def padleft(a, n, x)
  a.dup.reverse.fill(x, a.length..n-1).reverse
end

(destructive)

(破坏)

def padleft(a, n, x)
  a.reverse.fill(x, a.length..n-1).reverse
end

#1


23  

Edited due to my misunderstanding of the question. Pervious version of my answer padded from the right side, but the question was asking to do it from the left side. I corrected it accordingly. This is due to naming convention. ljust, rjust are builtin methods for String, and I extended that convention to Array, but that corresponds to padright and padleft, respectively, in the terminology of the question.

由于我对这个问题的误解而编辑。我的回答的前一个版本是从右边填充的,但问题是要从左边填充。我纠正它。这是由于命名约定。ljust, rjust是字符串的内建方法,我将这个约定扩展到数组,但是在问题的术语中,它分别对应于padright和padleft。

Destructive methods

破坏性的方法

def padleft!(a, n, x)
  a.insert(0, *Array.new([0, n-a.length].max, x))
end
def padright!(a, n, x)
  a.fill(x, a.length...n)
end

It would be more natural to have it defined on Array class:

在数组类中定义它会更自然:

class Array
  def rjust!(n, x); insert(0, *Array.new([0, n-length].max, x)) end
  def ljust!(n, x); fill(x, length...n) end
end

Non-destructive methods

非破坏性的方法

def padleft(a, n, x)
  Array.new([0, n-a.length].max, x)+a
end
def padright(a, n, x)
  a.dup.fill(x, a.length...n)
end

or

class Array
  def rjust(n, x); Array.new([0, n-length].max, x)+self end
  def ljust(n, x); dup.fill(x, length...n) end
end

#2


4  

Using 10 for the length to pad to, and 'x' to be what you're padding to, this pads right:

用10表示要填充的长度,x表示要填充的长度,这个垫子是对的:

>> asdf = %w[0 1 2 3 ] #=> ["0", "1", "2", "3"]
>> asdf += (asdf.size < 10) ? ['x'] * (10 - asdf.size) : [] #=> ["0", "1", "2", "3", "x", "x", "x", "x", "x", "x"]

or

>> asdf = (asdf.size < 10) ? ['x'] * (10 - asdf.size) + asdf : asdf #=> ["x", "x", "x", "x", "x", "x", "0", "1", "2", "3"]

to padleft

对padleft

If it makes sense to you to monkey-patch Array:

如果你觉得monkey-patch数组有意义:

class Array
  def pad_right(s, char=nil)
    self + [char] * (s - size) if (size < s)
  end

  def pad_left(s, char=nil)
    (size < s) ? [char] * (s - size) + self : self
  end
end

%w[1 2 3].pad_right(5, 'x') # => ["1", "2", "3", "x", "x"]
%w[1 2 3].pad_left(5, 'x') # => ["x", "x", "1", "2", "3"]

#3


4  

FWIW:

就其价值而言:

def rpad(item, padding, num)
  Array(item).fill padding, Array(item).size, num
end
# rpad "initialize value(s)", 0, 3
# => ["initialize value(s)", 0, 0, 0]

#4


2  

Using the * operator to repeat a list.

使用*操作符重复一个列表。

# Pad array to size n by adding x's. Don't do anything if n <= a.length.
def padleft(a, n, x)
  return a if n <= a.length
  return [x] * (n - a.length) + a
end

#5


1  

Perhaps more Rubyish ;)

也许更Rubyish;)

# Pad array to size n by adding x's. Don't do anything if n <= a.length.
def padleft(a, n, x)
  (n - a.size).times.inject(a) do |array, i|
    array << x
  end
end

#6


1  

If you're using Rails and want the padding on the right:

如果你正在使用Rails并且想要右边的填充:

[1,2,3,4,5,6].in_groups_of(4)
=> [[1, 2, 3, 4], [5, 6, nil, nil]]

This doesn't come anywhere near answering the question but it's what I ended up needing after visiting this page. Hope it helps someone.

这几乎不能回答这个问题,但这正是我在浏览完这一页后所需要的。希望它能帮助一些人。

#7


1  

Here's another fun one-liner to do it:

这是另一个有趣的俏皮话:

(non-destructive)

(无损)

def padleft(a, n, x)
  a.dup.reverse.fill(x, a.length..n-1).reverse
end

(destructive)

(破坏)

def padleft(a, n, x)
  a.reverse.fill(x, a.length..n-1).reverse
end