生成相邻子集的序列(从列表列表中)

发布于 2024-11-07 02:02:29 字数 387 浏览 2 评论 0原文

我有一个形式为的矩阵(数组的数组)

[1, 2, 3, 4]
[12, 23, 34]
[123, 234]
[1234]

并且想要生成这个矩阵的序列,该序列彼此跟随并且(转换为字符串)与索引 0(最顶部)处的数组的长度相同。因此,即结果将是

[1, 2, 3, 4]
[12, 3, 4]
[1, 23, 4]
[1, 2, 34]
[12, 34]
[123, 4]
[1, 234]
[1234]

我想要实现的事情是获取可以直接相互连接的字符串的所有部分,并将其分割为子数组(如示例所示)。

实现语言无关紧要,但最好是Python、java、ruby、C#、clojure、伪代码或其他相当高级的语言。

I have an matrix (array of arrays) in the form

[1, 2, 3, 4]
[12, 23, 34]
[123, 234]
[1234]

And want to produce sequences of this matrix that is following each other and is (converted to a string) in the same length as the array at index 0 (top-most). So i.e. the result of this would be

[1, 2, 3, 4]
[12, 3, 4]
[1, 23, 4]
[1, 2, 34]
[12, 34]
[123, 4]
[1, 234]
[1234]

The thing I want to achive is to get all parts of a string that can be directly connected to each other and splitted i sub arrays (as shown in the example).

The implementation language is irrelevant but preferably in i.e. Python, java, ruby, C#, clojure, Psudo code, or other language at a fairly high level.

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

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

发布评论

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

评论(5

暮凉 2024-11-14 02:02:29

我改进了之前的代码。

a = ["1", "2", "3", "4"]

['', '.'].repeated_permutation(a.length - 1).map{|b| a.zip(b).join.split('.')}

将为您提供:

[
  ["1234"],
  ["123", "4"],
  ["12", "34"],
  ["12", "3", "4"],
  ["1", "234"],
  ["1", "23", "4"],
  ["1", "2", "34"],
  ["1", "2", "3", "4"]
]

I improved the code from my previous one.

a = ["1", "2", "3", "4"]

['', '.'].repeated_permutation(a.length - 1).map{|b| a.zip(b).join.split('.')}

will give you:

[
  ["1234"],
  ["123", "4"],
  ["12", "34"],
  ["12", "3", "4"],
  ["1", "234"],
  ["1", "23", "4"],
  ["1", "2", "34"],
  ["1", "2", "3", "4"]
]
沉鱼一梦 2024-11-14 02:02:29

这是一个 Python 版本(经过编辑使其更加简洁;感谢 FMc 的建议):

def consecutive_slice(arr):
    yield arr
    mx = len(arr) + 1
    for size in xrange(2, mx):
        for i in xrange(mx - size):
            yield(arr[:i] + [''.join(arr[i:i+size])] + arr[i+size:])

使用示例:

>>> for seq in consecutive_slice(['1', '2', '3', '4']):
...     print(seq)
... 
['1', '2', '3', '4']
['12', '3', '4']
['1', '23', '4']
['1', '2', '34']
['123', '4']
['1', '234']
['1234']

Here's a Python version (edited to be more concise; thanks to FMc's suggestions):

def consecutive_slice(arr):
    yield arr
    mx = len(arr) + 1
    for size in xrange(2, mx):
        for i in xrange(mx - size):
            yield(arr[:i] + [''.join(arr[i:i+size])] + arr[i+size:])

Usage example:

>>> for seq in consecutive_slice(['1', '2', '3', '4']):
...     print(seq)
... 
['1', '2', '3', '4']
['12', '3', '4']
['1', '23', '4']
['1', '2', '34']
['123', '4']
['1', '234']
['1234']
橘亓 2024-11-14 02:02:29
def adj(ar)
  result = [ar]
  2.upto ar.size do |j|
    0.upto ar.size-j do |i|
      result << [*ar[0, i], ar[i,j].join.to_i, *ar[i+j..-1]]
    end
  end
  result
end

测试

a = [*1..5]
adj a
# [[1, 2, 3, 4, 5], 
#  [12, 3, 4, 5], 
#  [1, 23, 4, 5], 
#  [1, 2, 34, 5], 
#  [1, 2, 3, 45], 
#  [123, 4, 5], 
#  [1, 234, 5], 
#  [1, 2, 345], 
#  [1234, 5], 
#  [1, 2345], 
#  [12345]]

编辑

如果你希望它与 Ruby 1.8.7 一起工作(上部是 1.9+)

def adj(ar)
  result = [ar]
  2.upto ar.size do |j|
    0.upto ar.size-j do |i|
      result << [ar[0, i], ar[i,j].join.to_i, ar[i+j..-1]].flatten
    end
  end
  result
end

a = (1..5).to_a
adj a
#=> same result
def adj(ar)
  result = [ar]
  2.upto ar.size do |j|
    0.upto ar.size-j do |i|
      result << [*ar[0, i], ar[i,j].join.to_i, *ar[i+j..-1]]
    end
  end
  result
end

Test

a = [*1..5]
adj a
# [[1, 2, 3, 4, 5], 
#  [12, 3, 4, 5], 
#  [1, 23, 4, 5], 
#  [1, 2, 34, 5], 
#  [1, 2, 3, 45], 
#  [123, 4, 5], 
#  [1, 234, 5], 
#  [1, 2, 345], 
#  [1234, 5], 
#  [1, 2345], 
#  [12345]]

EDIT

If you want it work with Ruby 1.8.7 (upper is 1.9+)

def adj(ar)
  result = [ar]
  2.upto ar.size do |j|
    0.upto ar.size-j do |i|
      result << [ar[0, i], ar[i,j].join.to_i, ar[i+j..-1]].flatten
    end
  end
  result
end

a = (1..5).to_a
adj a
#=> same result
爱*していゐ 2024-11-14 02:02:29

哦,这是一个很酷的函数式(是吗?)单行解决方案:

a = [1,2,3,4]
result = [array]
2.upto(a.size){ |s| a.each_cons(s).with_index{ |g, i| result << [*(a-g)[0, i], g.join.to_i, *(a-g)[i..-1]] } }
result
# [[1, 2, 3, 4], 
#  [12, 3, 4], 
#  [1, 23, 4], 
#  [1, 2, 34], 
#  [123, 4], 
#  [1, 234], 
#  [1234]]

Oh, Here is a cool functional (is it?) one-line solution:

a = [1,2,3,4]
result = [array]
2.upto(a.size){ |s| a.each_cons(s).with_index{ |g, i| result << [*(a-g)[0, i], g.join.to_i, *(a-g)[i..-1]] } }
result
# [[1, 2, 3, 4], 
#  [12, 3, 4], 
#  [1, 23, 4], 
#  [1, 2, 34], 
#  [123, 4], 
#  [1, 234], 
#  [1234]]
桃扇骨 2024-11-14 02:02:29

(Ruby 版本)我相信这适用于您的矩阵的一般情况。我转换为字符串,这样排序效果会很好。 编辑以在生成的序列中包含 ['12', '34']。

m = [['1', '2', '3', '4'], ['12', '23', '34'], ['123', '234'], ['1234']]
m0 = m.first.join
seq = m.map do |row|
  ( 1..row.size ).map { |e| row.combination( e ).to_a }.flatten( 1 ).map do |a|
    m0.include?( str = a.join ) ? ( m0.delete( str ).chars.to_a + a ).sort : nil
  end.compact.uniq
end.flatten 1

(Ruby version) I believe that this works in the general case for your matrix. I converted to strings so the sort would work out nicely. Edited to include ['12', '34'] in the generated sequence.

m = [['1', '2', '3', '4'], ['12', '23', '34'], ['123', '234'], ['1234']]
m0 = m.first.join
seq = m.map do |row|
  ( 1..row.size ).map { |e| row.combination( e ).to_a }.flatten( 1 ).map do |a|
    m0.include?( str = a.join ) ? ( m0.delete( str ).chars.to_a + a ).sort : nil
  end.compact.uniq
end.flatten 1
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文