mardi 4 août 2015

Explain Line of Code

The program returns the largest common subsequence using recursion. However, I need someone to explain me :

return max(lcs_recursive(xlist, ys), lcs_recursive(xs, ylist), key = len)

What exactly, it returns and whether it is the same as just writing:

return max(len(lcs_recursive(xlist,ys), lcs_recursive(xs, ylist))

So basically, I'd like someone to explain me what exactly key = len does.

Code:

s1 = "abc"
s2 = "aebabc"

def lcs_recursive(xlist, ylist):
    if not xlist or not ylist:
        return []
    x, xs, y, ys = xlist[0], xlist[1:], ylist[0], ylist[1:]
    if x==y:
        return [x] + lcs_recursive(xs,ys)
    else:
        return max(lcs_recursive(xlist, ys), lcs_recursive(xs, ylist), key = len)
print lcs_recursive(s1, s2)

Aucun commentaire:

Enregistrer un commentaire