生成相邻子集的序列(从列表列表中)
我有一个形式为的矩阵(数组的数组)
[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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
我改进了之前的代码。
将为您提供:
I improved the code from my previous one.
will give you:
这是一个 Python 版本(经过编辑使其更加简洁;感谢 FMc 的建议):
使用示例:
Here's a Python version (edited to be more concise; thanks to FMc's suggestions):
Usage example:
测试
编辑
如果你希望它与 Ruby 1.8.7 一起工作(上部是 1.9+)
Test
EDIT
If you want it work with Ruby 1.8.7 (upper is 1.9+)
哦,这是一个很酷的函数式(是吗?)单行解决方案:
Oh, Here is a cool functional (is it?) one-line solution:
(Ruby 版本)我相信这适用于您的矩阵的一般情况。我转换为字符串,这样排序效果会很好。 编辑以在生成的序列中包含 ['12', '34']。
(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.