Table of Contents
ToggleQuestion
Consider a list (list = []
). You can perform the following commands:
insert i e
: Insert integer e at position i.print
: Print the list.remove e
: Delete the first occurrence of integer e.append e
: Insert integer e 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 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 3 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()
- Read the number of commands (
n
) from input. - Initialize an empty list
arr
to store the elements. - Iterate
n
times and read each command from input. - Split the command into a list of strings.
- 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 elemente
from the command and use theinsert
method to inserte
at indexi
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 theremove
method to remove the first occurrence ofe
from the list. - If the command is ‘append’, extract the element
e
from the command and use theappend
method to adde
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.
- If the command is ‘insert’, extract the index
- 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.