Lists Hackerrank
Consider a list (list = []). You can perform the following commands:
insert i e: Insert integer at position .print: Print the list.remove e: Delete the first occurrence of integer .append e: Insert integer 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 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.
Example
- : Append to the list, .
- : Append to the list, .
- : Insert at index , .
- : Print the array.
Output:
[1, 3, 2]
## Solutions:--
if __name__ == '__main__':
N = int(input())
list=[]
for i in range(N):
a=input().split()
#print(a)
for i in range(1,len(a)):
a[i]=int(a[i])
if a[0]=="insert":
list.insert(a[1],a[2])
elif a[0]=="print":
print(list)
elif a[0]=="remove":
list.remove(a[1])
elif a[0]=="append":
list.append(a[1])
elif a[0]=="sort":
list.sort()
elif a[0]=="pop":
list.pop()
elif a[0]=="reverse":
list.reverse()
Comments
Post a Comment