Consider a list (list = []
). You can perform the following commands:
insert i e
: Insert integer at position .print
: Print the list.remove e
: Delete the first occurrence of integer .append e
: Insert integer at the end of the list.sort
: Sort the list.pop
: Pop the last element from the list.reverse
: Reverse the list.Initialize your list and read in the value of followed by lines of commands where each command will be of the types listed above. Iterate through each command in order and perform the corresponding operation on your list.
if __name__ == '__main__':
N = int(raw_input())
List = []
commList = []
for i in range(0, N):
commList.append(str(raw_input()))
for commStr in commList:
cList = commStr.split()
comm = cList[0]
if comm == 'insert' :
List.insert(int(cList[1]), int(cList[2]))
elif comm == 'print' :
print List
elif comm == 'remove':
List.remove(int(cList[1]))
elif comm == 'append' :
List.append(int(cList[1]))
elif comm == 'sort' :
List.sort()
elif comm =='pop' :
List.pop()
elif comm == 'reverse':
List.reverse()