The title is a little weird, but I don't know exactly how this is called, so plz forgive me with the abstract title....
I've found a code like this online:
def lcs(xstr, ystr):
"""
>>> lcs('thisisatest', 'testing123testing')
'tsitest'
"""
if not xstr or not ystr:
return ""
x, xs, y, ys = xstr[0], xstr[1:], ystr[0], ystr[1:]
if x == y:
return x + lcs(xs, ys)
else:
return max(lcs(xstr, ys), lcs(xs, ystr), key=len)
I am new to python, and I don't understand how you can call lcs(xs, ys) in
return x + lcs(xs, ys)
To my understanding, lcs() is not yet fully defined, and I'm confused how you can call a function of itself inside itself....
Also, I don't know what key = len is doing in
max(lcs(xstr, ys), lcs(xs, ystr), key=len)
I know how max( 1st, 2nd ) works, but I don't know what the third parameter is doing. What does "key" mean, and why is "len" used as a value of "key"?