Lists in Python - Hacker Rank Solution

Problem 

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

  1. insert i e: Insert integer e at position i.
  2. print: Print the list.
  3. remove e: Delete the first occurrence of integer e.
  4. append e: Insert integer e 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 n followed by n 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.


Example





  • : Append  to the list, .
  • : Append  to the list, .
  • : Insert  at index .
  • : Print the array.
    Output:


[1, 3, 2]


Input Format

The first line contains an integer, , denoting the number of commands.

Each line  of the  subsequent lines contains one of the commands described above.


Constraints

The elements added to the list must be integers.

Output Format

For each command of type print, print the list on a new line.


Sample Input 0

12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print


Sample Output 0

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]


Explanation

First we take the input from the user in the form of lists and append to another list named lists.

Now, then we iterate the main list named lists and then check  the lists of first element that is equal to which function? {eg: if it equal to insert, print, or ...............etc.}.

Also, make a another lists that named arr that contain the elements according to the input given by the user.

And then according to function we apply operations.

For insert we use insert() function to insert the element in specific position.

For print we simply use print() function.

For remove we use remove() function to remove given element.

For append we use append() function to append elements in the arr lists.

For sort we use sort() function.

For pop we use pop() function to remove the last element from the list named arr.

And, lastly for reverse we use reverse() function to reverse the list.



Solution

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
if __name__ == '__main__':
    N = int(input())
    
    lists = []
    
    for i in range(N):
        a = list(map(str,input().split( )))
        lists.append(a)
    
    arr = []
    
    for x in lists:
        if x[0] == "insert":
            i = int(x[1])
            e = int(x[2])
            arr.insert(i,e)
        
        elif x[0] == "print":
            print(arr)
             
        elif x[0] == "remove":
            e = int(x[1])
            arr.remove(e)
             
        elif x[0] == "append":
            e = int(x[1])
            arr.append(e)
            
        elif x[0] == "sort":
            arr.sort()
            
        elif x[0] == "pop":
            arr.pop()
            
        elif x[0] == "reverse":
            arr.reverse() 
        
Next Post Previous Post
No Comment
Add Comment
comment url