0

Lets say there are two lists

L1=[['A', ['C', ['B', ['D', 0]]]],
   [['A', ['D', ['K', ['C', ['E', 0]]]]],
   [['A', ['C', ['B', ['M', 0]]]]]

and

L2=[['A', ['C', ['B', ['K', 0]]]],
   [['A', ['C', ['B', ['B', ['E', 0]]]]],
   [['A', ['D', ['K', ['F', 0]]]]]

Then the output should return all the sub-paths with longest common path. For example:

Since 'A', 'C', 'B' is common L1 and L2; output should be:

[['A', ['C', ['B', ['D', 0]]]],
[['A', ['C', ['B', ['M', 0]]]],
[['A', ['C', ['B', ['K', 0]]]],
[['A', ['C', ['B', ['B', ['E', 0]]]]]

. Also, 'A', 'D', 'K' is also common for one time in L1 and L2; the output whould be:

[['A', ['D', ['K', ['C', ['E', 0]]]]],
[['A', ['D', ['K', ['F', 0]]]]]

I tried :

[i for i in L1 if i in L2]

but it will give the output of all the common paths till the leaf (end).

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • It is not clear to me what happens in edge cases, e.g. will you output similar paths only if they appear in both lists? What if there are no similar paths? Also, what is the desired output format (set, list, dict, etc.)? – DocDriven Aug 06 '19 at 11:39
  • The output will be the both the paths in two lists with longest common sequence. As mentioned above in the example, since A C & B was common in both the lists, thus both the list items containing these common longest sub-sequence came in output. The output may be a list or the count of number of such items. For no similar path the output is zero – churchurnaan Aug 06 '19 at 14:12

1 Answers1

0

take something from the great and marvelous c!

you can simply use a while in another and check letter by letter, if one is different exit the first while and put the character to 0

like

while (var[i][n] && var2[i][n])
   while (var[i][n] == var2[i][n])
      n = n + 1
   var[i][n] = 0

or something like that. but it is not really optimised.