Question

I am soo close to finishing my program, but I am having a small issue. The I/O is suppose to look like this:

I: fanlc2("human", "chimpanzee")
O: [4, 'h#man', '#h#m#an###']

BUT mine, does the following:

I: fanlc2("human", "chimpanzee")
O: [4, '#', '#h#']

Can someone please edit my code so I get the proper answer. I cant figure it out. Thanks:

def fanlc2(S1, S2):
    if S1 == '' or S2 == '':
        return [0, S1, S2]
    if S1[0] == S2[0]:
        temp = fanlc2(S1[1:], S2[1:])
        return [temp[0]+1, S1[0]+temp[1], S2[0]+temp[2]]
    t1 = fanlc2(S1[1:], S2)
    t2 = fanlc2(S1, S2[1:])
    if t1[0] > t2[0] or t1[0] == t2[0]:
        return [t1[0], '#'+t1[1], t2[1]]
    return [t2[0], t1[1], '#'+t2[1]]

Here are more I/O's that the program SHOULD be returning:

fanlc2("x", "y")

[0, '#', '#']

fanlc2("spam", "")

[0, '####', '']

fanlc2("spa", "m")

[0, "###", "#"]

fanlc2("cat", "car")

[2, "ca#, "ca#"]

fanlc2("cat", "lca")

[2, "ca#", "#ca"]

fanlc2("human", "chimpanzee")

[4, 'h#man', '#h#m#an###']

Was it helpful?

Solution

You just mixed up some of your variables/indexing in the last couple lines. Try using more descriptive variable names and you may avoid this problem in the future.

if t1[0] > t2[0] or t1[0] == t2[0]:
    return [t1[0], '#'+t1[1], t1[2]]
return [t2[0], t2[1], '#'+t2[2]]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top