hackerrank -python --List 1

Consider a list (list = []). You can perform the following commands:

  1. insert i e: Insert integer  at position .
  2. print: Print the list.
  3. remove e: Delete the first occurrence of integer .
  4. append e: Insert integer  at the end of the list.
  5. sort: Sort the list.
  6. pop: Pop the last element from the list.
  7. 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()

 

你可能感兴趣的:(python)