Interleaved sequences represent an arbitrary merging of some number of sequences.
An interleaved sequence can be made by appending elements to a list one by one from some number of lists, choosing the next element from some list each time. Therefore, an interleaved sequence will contain exactly the same elements of all the lists combined, in an order consistent with all lists.
The only interleaving of 1 list is that same list.
Challenge
Your challenge is to create a function/program that takes an arbitrary number of sequences and outputs all possible interleavings of those sequences.
Examples
Input: [1, 2], [3, 4]
Output:
[1, 2, 3, 4]
[1, 3, 2, 4]
[1, 3, 4, 2]
[3, 1, 2, 4]
[3, 1, 4, 2]
[3, 4, 1, 2]
Input: [1, 2, 3, 4, 5]
Output:
[1, 2, 3, 4, 5]
Input: []
Output:
[]
Input: <nothing>
Output:
[]
(also acceptable)
Input: <nothing>
Output: <nothing>
Input: [1, 2, 3], [4, 5]
Output:
[1, 2, 3, 4, 5]
[1, 2, 4, 3, 5]
[1, 2, 4, 5, 3]
[1, 4, 2, 3, 5]
[1, 4, 2, 5, 3]
[1, 4, 5, 2, 3]
[4, 1, 2, 3, 5]
[4, 1, 2, 5, 3]
[4, 1, 5, 2, 3]
[4, 5, 1, 2, 3]
Input: [1, 2], [3, 4], [5, 6]
Output:
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 5, 4, 6]
[1, 2, 3, 5, 6, 4]
[1, 2, 5, 3, 4, 6]
[1, 2, 5, 3, 6, 4]
[1, 2, 5, 6, 3, 4]
[1, 3, 2, 4, 5, 6]
[1, 3, 2, 5, 4, 6]
[1, 3, 2, 5, 6, 4]
[1, 3, 4, 2, 5, 6]
[1, 3, 4, 5, 2, 6]
[1, 3, 4, 5, 6, 2]
[1, 3, 5, 2, 4, 6]
[1, 3, 5, 2, 6, 4]
[1, 3, 5, 4, 2, 6]
[1, 3, 5, 4, 6, 2]
[1, 3, 5, 6, 2, 4]
[1, 3, 5, 6, 4, 2]
[1, 5, 2, 3, 4, 6]
[1, 5, 2, 3, 6, 4]
[1, 5, 2, 6, 3, 4]
[1, 5, 3, 2, 4, 6]
[1, 5, 3, 2, 6, 4]
[1, 5,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3, 1, 6, 4, 2]
[5, 3, 4, 1, 2, 6]
[5, 3, 4, 1, 6, 2]
[5, 3, 4, 6, 1, 2]
[5, 3, 6, 1, 2, 4]
[5, 3, 6, 1, 4, 2]
[5, 3, 6, 4, 1, 2]
[5, 6, 1, 2, 3, 4]
[5, 6, 1, 3, 2, 4]
[5, 6, 1, 3, 4, 2]
[5, 6, 3, 1, 2, 4]
[5, 6, 3, 1, 4, 2]
[5, 6, 3, 4, 1, 2]
Rules
- Standard loopholes forbidden (duh)
- Input may be taken in any reasonable format, e.g. a list of lists, vararg list of lists, parameter lists, etc... as long as it is unambiguous where lists begin and end.
- Output may be in any reasonable format, so long as it is clear where lists begin and end. Valid outputs include, but are not necessarily limited to:
- stdout, with one list per line
- A list of lists
- An iterator over lists (can be implemented with a generator if your language has them)
- The order of the yielded interleavings does not matter, however, there should not be any repeated interleavings.
- To simplify repeat detection, you may assume that all elements across all input sequences are unique.
- If given no lists as input, both the empty list and no output are valid outputs.
- Types of the elements in the sequences is irrelevant. (e.g. they could all be one type or a mishmash of types, whichever is more convenient in your language)
- Your program/function must be guaranteed to terminate in a finite amount of time.
- This is code-golf, so the shortest code for each language wins.
[[]]
instead of[]
when we are given no lists as input? \$\endgroup\$