Challenge:
Given a list of nonempty lists of integers, return a list of tuples of the following form: First list tuples starting with each element of the first list followed by the first element of every subsequent list, so the ith tuple should be [ith element of first list, first element of second list, ... , first element of last list]
. For example:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] => [[1, 4, 7], [2, 4, 7], [3, 4, 7], ...
Then do tuples of the form [last element of first list, ith element of second list, first element of third list, ..., first element of last list]
, so in our example this would be:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] => ..., [3, 4, 7], [3, 5, 7], [3, 6, 7], ...
Continue on with each remaining list, until you get to [last element of first list, ..., last element of second to last list, ith element of last list]
:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] => ..., [3, 6, 7], [3, 6, 8], [3, 6, 9]]
The full output is as follows:
[[1, 2, 3], [4, 5, 6], [7, 8, 9]] =>
[[1, 4, 7], [2, 4, 7], [3, 4, 7], [3, 5, 7], [3, 6, 7], [3, 6, 8], [3, 6, 9]]
Some boilerplate for good measure:
- If you want the input to be lists of strings, or lists of positive integers, it's fine. The question is about manipulating lists, not about what is in the lists.
- Input and output can be in any acceptable format.
- Either a full program or function is permitted.
- Standard loopholes are disallowed by default.
- This question is code golf, so lowest byte-count wins.
Examples:
[] => [[]] (or an error, thanks to ngn for correcting the output in this case)
[[1]] => [[1]]
[[1, 2], [3, 4], [5]] => [[1, 3, 5], [2, 3, 5], [2, 4, 5]]
[[1], [2], [5, 6], [3], [4]] => [[1, 2, 5, 3, 4], [1, 2, 6, 3, 4]]
[[1, 2, 3], [4, 5]] => [[1, 4], [2, 4], [3, 4], [3, 5]]
[[1, 2, 3], []] => unspecified behavior (can be an error)
[[3, 13, 6], [9, 2, 4], [5, 10, 8], [12, 1, 11], [7, 14]] =>
[[3, 9, 5, 12, 7], [13, 9, 5, 12, 7], [6, 9, 5, 12, 7], [6, 2, 5, 12, 7],
[6, 4, 5, 12, 7], [6, 4, 10, 12, 7], [6, 4, 8, 12, 7], [6, 4, 8, 1, 7],
[6, 4, 8, 11, 7], [6, 4, 8, 11, 14]]
[[16, 8, 4, 14, 6, 7, 10, 15], [11, 1, 12, 2, 19, 18, 9, 3], [13, 5, 17]] =>
[[16, 11, 13], [8, 11, 13], [4, 11, 13], [14, 11, 13], [6, 11, 13],
[7, 11, 13], [10, 11, 13], [15, 11, 13], [15, 1, 13], [15, 12, 13], [15, 2, 13],
[15, 19, 13], [15, 18, 13], [15, 9, 13], [15, 3, 13], [15, 3, 5], [15, 3, 17]]
If anyone has a better title, let me know.
[] => []
should really be[] => [[]]
but can't find the words to explain why. \$\endgroup\$[[]]
because there is a single empty tuple with one entry from each of the (zero) sublists. Probably it's too annoying to require programs to correctly output this, so I'll say that it's not necessary. \$\endgroup\$[]
is, strictly speaking, an empty list of non-empty lists but the output is ambiguous between[]
and[[]]
if it's an allowed input. ("First list tuples starting with each element of the first list..." - there is no first list, so we are done ->[]
) \$\endgroup\$[]
should be[[]]
. For instance, the number of output tuples issum(inner list lengths) - length of outer list + 1
which in the empty case gives1
, which is the length of[[]]
but not the length of[]
. This is a bit of a pedantic issue though... \$\endgroup\$