HackerRank Python: Lists Solution

Table of Contents

Question

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 7 types listed above. Iterate through each command in order and perform the corresponding operation on your list.

Example

N = 4
append 1
append 2
insert 3 1
print

  • append 1: Append 1 to the list, arr = [1].
  • append 2: Append 2 to the list, arr [1, 2].
  • insert 3 1: Insert at index 1,arr = [1, 3, 2].
  • print: Print the array.
[1, 3, 2]

Input Format

The first line contains an integer, n, denoting the number of commands.
Each line i of the n 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]

Solution

				
					n = int(input())
arr = []

for _ in range(n):
    command = input().split()
    if command[0] == 'insert':
        i = int(command[1])
        e = int(command[2])
        arr.insert(i, e)
    elif command[0] == 'print':
        print(arr)
    elif command[0] == 'remove':
        e = int(command[1])
        arr.remove(e)
    elif command[0] == 'append':
        e = int(command[1])
        arr.append(e)
    elif command[0] == 'sort':
        arr.sort()
    elif command[0] == 'pop':
        arr.pop()
    elif command[0] == 'reverse':
        arr.reverse()

				
			
  1. Read the number of commands (n) from input.
  2. Initialize an empty list arr to store the elements.
  3. Iterate n times and read each command from input.
  4. Split the command into a list of strings.
  5. Based on the first element of the command, perform the corresponding operation on the list:
    • If the command is ‘insert’, extract the index i and element e from the command and use the insert method to insert e at index i in the list.
    • If the command is ‘print’, print the list.
    • If the command is ‘remove’, extract the element e from the command and use the remove method to remove the first occurrence of e from the list.
    • If the command is ‘append’, extract the element e from the command and use the append method to add e to the end of the list.
    • If the command is ‘sort’, use the sort method to sort the list in ascending order.
    • If the command is ‘pop’, use the pop method to remove and return the last element from the list.
    • If the command is ‘reverse’, use the reverse method to reverse the elements in the list.
  6. After performing all the commands, the resulting list will be printed when the ‘print’ command is encountered.

If you find anything wrong in this Solution, feel free to reach us in the comment section.

Sharing Is Caring:

Leave a Comment