1. Problem statement
Given a certain rectangular matrix I want to scan its rows from left right to find equally sized sequences. These sequences should be as short as possible and unique, so that they can be used as Keys
to associate the remaining row members. A FirstShortestUncommonSequence
, so to speak.
2. Test data
list =
{{2, 4, 1, 7, 1, 4, 1},
{1, 2, 3, 7, 2, 4, 0},
{1, 4, 1, 7, 3, 5, 1},
{2, 4, 1, 7, 4, 5, 0},
{1, 1, 1, 7, 5, 5, 3}}
3. Example code
The following code is terrible (it doesn't even have an exit clause), and I only show it to give some examples.
CreateKeys[list_?MatrixQ] :=
Module[{p},
p =
First @ FirstPosition[True]@
Table[
Length @ DeleteDuplicates[#] == Length[#] & [list[[All, ;; i]]],
{i, Length @ First @ list}];
If[NumberQ[p],
AssociationThread[list[[All, ;; p]] -> list[[All, p + 1 ;;]] /. {a_} :> a],
p]]
4. Examples of expected result
CreateKeys @ list
should give
<|{2, 4, 1, 7, 1} -> {4, 1},
{1, 2, 3, 7, 2} -> {4, 0},
{1, 4, 1, 7, 3} -> {5, 1},
{2, 4, 1, 7, 4} -> {5, 0},
{1, 1, 1, 7, 5} -> {5, 3}|>
o
CreateKeys[{{1, 1, 4}, {2, 1, 5}}]
<|1 -> {1, 4}, 2 -> {1, 5}|>
o
CreateKeys[{{1, 1, "a", "b"}, {1, 5, "a", "d"}}]
<|{1, 1} -> {"a", "b"}, {1, 5} -> {"a", "d"}|>
and, if no unique keys can be found,
CreateKeys[{{1, 1}, {1, 1}}]
"NotFound"
5. Question
How can this problem be solved in a functional and efficient way?