def flight_sequences_overlap(path1: List[str], path2: List[str]) -> bool: """Return True iff flight sequences path1 and path2 overlap: if the last x number of stops (where x >= 1) of path1 are identical to the first x number of stops of path2. Precondition: - both path1 and path2 are valid flight sequences - the airport codes in each flight path is unique (i.e. we will never have a path where we visit the same airport more than once) >>> path1 = ['AA1', 'AA2'] >>> path2 = ['AA2', 'AA3'] >>> flight_sequences_overlap(path1, path2) True >>> path3 = ['AA1', 'AA2', 'AA3'] >>> path4 = ['AA2', 'AA3', 'AA4'] >>> flight_sequences_overlap(path3, path4) True >>> path5 = ['AA2', 'AA3'] >>> path6 = ['AA1', 'AA3', 'AA2'] >>> flight_sequences_overlap(path5, path6) False """ pass
Types of Linked List
A sequence of data elements connected through links is called a linked list (LL). The elements of a linked list are nodes containing data and a reference to the next node in the list. In a linked list, the elements are stored in a non-contiguous manner and the linear order in maintained by means of a pointer associated with each node in the list which is used to point to the subsequent node in the list.
Linked List
When a set of items is organized sequentially, it is termed as list. Linked list is a list whose order is given by links from one item to the next. It contains a link to the structure containing the next item so we can say that it is a completely different way to represent a list. In linked list, each structure of the list is known as node and it consists of two fields (one for containing the item and other one is for containing the next item address).
def flight_sequences_overlap(path1: List[str], path2: List[str]) -> bool:
"""Return True iff flight sequences path1 and path2 overlap: if the last
x number of stops (where x >= 1) of path1 are identical to the
first x number of stops of path2.
Precondition:
- both path1 and path2 are valid flight sequences
- the airport codes in each flight path is unique (i.e. we will
never have a path where we visit the same airport more than once)
>>> path1 = ['AA1', 'AA2']
>>> path2 = ['AA2', 'AA3']
>>> flight_sequences_overlap(path1, path2)
True
>>> path3 = ['AA1', 'AA2', 'AA3']
>>> path4 = ['AA2', 'AA3', 'AA4']
>>> flight_sequences_overlap(path3, path4)
True
>>> path5 = ['AA2', 'AA3']
>>> path6 = ['AA1', 'AA3', 'AA2']
>>> flight_sequences_overlap(path5, path6)
False
"""
pass
Step by step
Solved in 4 steps with 3 images