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
[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]
Answer
if __name__ == '__main__':
N = int(input())
commands = {
"insert": lambda x, y, z: x.insert(y, z),
"print": lambda x: print(x),
"remove": lambda x, y: x.remove(y),
"append": lambda x, y: x.append(y),
"sort": lambda x: x.sort(),
"pop": lambda x: x.pop(),
"reverse": lambda x: x.reverse(),
}
out = []
for i in range(N):
a = input()
split_a = a.split(' ')
command = split_a[0]
try:
commands[command](out, int(split_a[1]), int(split_a[2]))
except IndexError:
try:
commands[command](out, int(split_a[1]))
except IndexError:
commands[command](out)
Copyrights
If you have any copyrights issues/complaints related to this article, you may send us mail at kaustubh@outlook.in, Also please refer to Terms and Conditions and Privacy Policy.