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()

 

發佈了27 篇原創文章 · 獲贊 1 · 訪問量 5469
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章