(I haven't rigorously tested it).
Well you should write some test to ensure validity of the function. So even after changes you can be sure it will still work.
doctest is a pretty nice module to use, and is a nice extension of your docstring
Variables should have descriptive names!
lst2 if it wasn't for that docstring I would not have known what is the subseq and the parent, so instead I propose to rename them to
haystack here the intent is more clear
Same goes for
d2... I can see that they are the remaining length of the list, but it is hard to tell from the variable name.
for is considered more Pythonic vs
For loops are Pythons greatest feature IMHO, they are easy to read and short to write
You should start writing for loops instead of a while, "Loop like a native" might be an interesting talk to view
Too many assignments in a line
Might be preference, but I find this line hard to read:
i, j, d1, d2 = i+1, j+1, d1-1, d2-1
There are too many values with not enough descriptive names on this line
We can instead loop over the
haystack and use slicing to compare the sliced
haystack with the
needle, lastly top it off with the
any keyword and write some tests with the doctest module
def is_subsequence(needle, haystack):
Finds if a list is a subsequence of another.
needle: the candidate subsequence
haystack: the parent list
>>> is_subsequence([1, 2, 3, 4], [1, 2, 3, 4, 5, 6])
>>> is_subsequence([1, 2, 3, 4], [1, 2, 3, 5, 6])
>>> is_subsequence(, [1, 2, 3, 5, 6])
>>> is_subsequence([5, 6], [1, 2, 3, 5, 6])
>>> is_subsequence([[5, 6], 7], [1, 2, 3, [5, 6], 7])
>>> is_subsequence([1, 2, 3, 4, 5, 6, 7, 8], [1, 2, 3, [5, 6], 7])
haystack[i:i+len(needle)] == needle
for i in range(len(haystack) - len(needle) + 1)
if __name__ == '__main__':