Given a list of list of integers, task is to write a function which will output another list of list of integers. It's hard to tell the nature of the desired output in words, I am writing some examples:
# list with one element will be returned
>>> func([[1, 2, 3, 4]])
[[1, 2, 3, 4]]>>> func([[1], [1, 2]])
[[1], [2]]>>> func([[1, 2], [1]])
[[1], [2]]>>> func([[1], [1, 2, 3]])
[[1], [2, 3]]>>> func([[1], [1, 2, 3], [1, 4]])
[[1], [2, 3], [4]]>>> func([[1, 2], [2, 3]])
[[1], [2], [3]]>>> func([[1], [2], [1, 2, 3]])
[[1], [2], [3]]>>> func([[1, 2], [1, 2, 3]])
[[1, 2], [3]]>>> func([[1], [1, 2], [1, 2, 3]])
[[1], [2], [3]]
(UPDATE) You can use the following preconditions:
Each inner list contains integers already sorted and there is no duplicate entry.
The outer list has no duplicate entry.
(UPDATE) As you asked, here's what I think I'm stuck with:
This is a problem on some optimization on a Directed Graph, with numbers as nodes and inner lists as starting points of edges (the outer list is the set of starting points of all edges). Now, you might ask, "How there be multiple start points to a single edge, which is shown in some test cases?"
This is what I am trying to do: for func ([1, 2])
the node 1 & node 2 can be merged to a single node. The output [1, 2]
shows that these two can be merged.
Now look at func ([[1], [1, 2]])
. The 2nd inner list tries to merge node 1 & 2 together, but the 1st inner list says node 1 cannot be merged to anything. So, the output is [[1], [2]]
, indicating that node 1 & node 2 are to be kept separated.
For func ([[1], [2, 3], [1, 2, 3]])
, node 1 is to be separated, but node 2 & 3 can be merged; so the output would be [[1], [2, 3]]
In case of func ([[1, 2], [2, 3]])
, neither node 1 & 2 nor 2 & 3 can be merged as node 1 & 3 can be merged, so the expected result is [[1], [2], [3]]
.
There is a list of integers also, which comprises of end points of vertices, each integer corresponds to each inner list. When elements of an inner list are merged to one, there remains only 1 edge. When they are separated, there are singleton lists, and elements from each of which is taken as starting points; the list of end points are updated accordingly.
I think it will help you to realize my needs.