I have already submitted a draft code for the LCS problem of two sequences. I made bad mistakes trying greedy, now I have implemented I believe a stable greedy algorithm for this problem. Though I have two issues, this part of an online course, when I submit it it says for sequences [1,2,3] and [3,2,1] the correct output is 1, where I believe Why? So, I went to the correct versions and tested, it works fine, that is the output is 0, and tested it for many good test cases against the correct versions, it works. Now, I have threequestions: Why [1,2,3] and [3,2,1] should output 1 instead of 0? If my code is not valid, please help me with some test cases that invalidate it? Thanks! My code:
def lcs2(a, b):
lst, tag= [], False
if len(set(a).intersection(set(b))) != 0: tag = True
if len(a) <= len(b): x = a; y = b
else: x = b; y = a
for i in y:
if i in x:
lst.append(x.index(i))
x[x.index(i)] = None
else:
y[i] = None
cnt = 0
for i in range(1 ,len(lst)):
if lst[i] > lst[i-1]: cnt += 1
if cnt == 0 and not tag: return 0
return cnt + 1
Now, I take this one here, some proposed seemed to implement a good one:Python: Length of longest common subsequence of lists
But when testing it for a = [-1, 1, -1, -1, 4]
b = [0, -1, 0, 4, 4]
it just fails against me, which gives the correct answer 2 rather than one.