I'm trying to figure out a way to check if order exists in a sequence of elements. For example:
orderExists = [
['a','c','d','e'],['b','d','f'],['a','b','c','e']
]
orderViolation = [
['a','c','d','e'],['b','d','f'],['a','e','c','b']
]
Order exists because the valid sequence should look like a,b,c,d,e,f
.
In sublists the elements may be absent.
For example in orderExists[0] = ['a','c','d','e']
b
and f
are absent but as long as c
comes after a
and is before d
order is respected.
Order is violated because in orderViolation[2] = ['a','e','c','b']
e
should not be before c
.
Also this is a test case, i don't know the valid sequence, the only thing that i have is a list of sublists as in the example above.
Is there any effective way to find out if there order exists or not?
NOTE: The sublists do not contain only elements in alphabetical order.
For Example: New York --> Los Angeles --> Chicago
orderExists = [
['Los Angeles', 'Chicago'],
['New York','Los Angeles'],
['New York','Chicacgo']
]