Your task is to implement a list data structure that supports efficient addition to the beginning and to the end of the list, and the reversal of the order of the elements.
In a file fliplist.py
, implement a class FlipList
with the following methods:
add_first(x)
: add the elementx
to the beginning of the listadd_last(x)
: add the elementx
to the end of the listflip()
: reverse the order of the elements
All the methods above should be efficient. In addition, the method __repr__
should return the contents of the list as a string as shown in the following examples.
class FlipList: def __init__(self): # TODO def __repr__(self): # TODO def add_first(self, x): # TODO def add_last(self, x): # TODO def flip(self): # TODO if __name__ == "__main__": numbers = FlipList() numbers.add_last(1) numbers.add_last(2) numbers.add_last(3) print(numbers) # [1, 2, 3] numbers.add_first(4) print(numbers) # [4, 1, 2, 3] numbers.flip() print(numbers) # [3, 2, 1, 4] numbers.add_last(5) print(numbers) # [3, 2, 1, 4, 5]