user13538781
user13538781

Reputation:

Need help finding an indentation/syntax error in my list code

Okay, so I'm working through a doctest to check all of my code. I have about five other solutions that work fine - so the meat and potatoes of my code is functional. However, I am failing one test constantly, and getting False instead of True. Below is my code

def duplist(lst1, lst2):
    lst1Counter = 0
    lst2Counter = 0
    while lst1Counter in range(len(lst1)) and lst2Counter in range(len(lst2)):
        if lst1[0] == lst2[0]:
            lst1Counter+=1
            lst2Counter+=1
        else:
            lst2Counter+=1

    if lst1Counter not in range(len(lst1)):
        return True
    else:
        return False

The point of the list is to see if the first list is a sublist of the second. It's all set except for one test.

duplist([15,1,100],[20,15,30,50,1,100])==True

I always return False. It's got to be a small syntax/indentation error - but I can't find it anywhere. Can someone point me in the right direction?

Upvotes: 0

Views: 38

Answers (1)

anveshjhuboo
anveshjhuboo

Reputation: 168

Visualizing what your code is doing might point you in the right direction. Let's review the code:

For duplist([15, 1, 100], [20, 15, 30, 50, 1, 100]), after the completion of the while loop, you are left with:

lst1Counter = 0
lst2Counter = 3  # for 3 iterations where lst1[0] != lst2[0] in the above case

Then you are looking whether lst1Counter not in range(len(lst1)) which is:

if 0 not in [0, 1, 2]

That is a false statement, since 0 is in [0, 1, 2]. Hence, it returns False instead of True.

Also, let us know if you need some help with your code, it is not actually checking whether a lst1 is a sublist of lst2.

Hope this helped!

Upvotes: 1

Related Questions