Skip to content

Instantly share code, notes, and snippets.

@eeddaann
Created March 29, 2018 19:49
Show Gist options
  • Save eeddaann/6cc3772658ace4a75d9745d9733b2089 to your computer and use it in GitHub Desktop.
Save eeddaann/6cc3772658ace4a75d9745d9733b2089 to your computer and use it in GitHub Desktop.
def is_contained(sub,lst):
last_element_index = -1 # dummy value
if set(sub) > set(lst): # check if sub is subset of lst
return False
for i in range(len(sub)): # iterate over sub with for-loop
if sub[i] in lst: # check if element i in sub exists in lst
lst_indexes = [j for j, y in enumerate(lst) if y == sub[i]] # list of all occurrences of sub[i] in lst
if max(lst_indexes) > last_element_index + 1: # check if relevant elements are in ascending order
last_element_index = min(lst_indexes) # update last index
else:
return False
else:
return False
return True
print(is_contained([1,2,3],[0,1,0,2,0,0,3]))
# True
print(is_contained([1,1],[1,2,3,2,1]))
# True
print(is_contained([2,1],[1,2,3]))
# False
print(is_contained([2,1],[1,2,3,1]))
# True
print(is_contained([1,1],[1,2,3]))
# False
print(is_contained([],[1]))
# True
print(is_contained([1],[0]))
# False
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment