Basically for an assignment I need to determine if a list is a sublist of another list, which I saw was answered in this post Checking if list is a sublist, however there are some special requirements, that will be explained in the examples, that werent answered in that post. I also saw this post How to check if a list is in another list with the same order python which also didnt help determine the special requirements.
Example1:
list1 = 'a,q,b,q,q,q,q,q,q,c'
sublist = 'a,b,c'
output -> True
Explanation: Now, I know this isn't necessarily a sublist of list1, but a, b, and c are all present in list1, in the same order as the sublist variable, with only q separating them, which I can ignore because q isnt in sublist, which is why this would output true.
Example2:
list1 = 'b,q,q,q,q,a,q,c'
sublist = 'a,b,c'
output -> False
Explanation: Although this list does contain a, b, and c like the other example, it is out of order, hence why it would be false
Example3:
list1 = 'a,b,b,q,c'
sublist = 'a,b,c'
output -> False
Example4:
list1 = 'a,b,b,a,b,q,c'
sublist = 'a,b,c'
output -> True
Explanation: At the beginning of the list we have a,b,b which in the before explanation I stated was false, however following that part we have the correct order of a,b,c with a q separating the b and c
Here is my code. The problem I can't seem to find shows up in one hidden test case I have to run this through, I cant see the input or the output, which is making it hard to debug this. I've tried to run many of my own different test cases, but I can't seem to find what I'm missing here. I'm just wondering if anyone can figure out what I'm forgetting to take into account when running this.
sublist = 'a,b,c'.split(',')
l1 = 'a,q,b,a,q,q,q,q,q,q,q,q,q,c'.split(',')
item_order = []
for item in l1:
#Check if I have found a sublist
if item_order == sublist:
break
#If item is in sublist, but hasnt been seen yet
elif item in sublist and item not in item_order:
item_order.append(item)
print('adding item', item_order)
#If item has been seen and is in sublist
elif item in item_order:
#Reset and add duplicated item
item_order = []
item_order.append(item)
print('found duplicate, resetting list', item_order)
if item_order == sublist: print("true")
else: print("false")