Task
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.
Solution:
if __name__ == '__main__': N = int(input()) L=[]; for i in range(0,N): cmd=input().split(); if cmd[0] == "insert": L.insert(int(cmd[1]),int(cmd[2])) elif cmd[0] == "append": L.append(int(cmd[1])) elif cmd[0] == "pop": L.pop(); elif cmd[0] == "print": print(L) elif cmd[0] == "remove": L.remove(int(cmd[1])) elif cmd[0] == "sort": L.sort(); else: L.reverse(); |
Note:
This Code is Verified by all Test Cases.If any error occurs then Comment correct code Below in comment box.
Disclaimer:-
The above hole problem statement is given by hackerrank.com, but the solution is generated by the CodexRitik . if any of the query regarding this post or website fill the following contact form Thank You.
Thanks Man
ReplyDeletebest knowledge for the article Sir thanks.
ReplyDeleteThanks Man
DeleteLINE 3 INDENTATION ERROR
ReplyDeleteWow NYC information sir ji
ReplyDeletegood
compilation error in elif cmd[0]=="append":------ it is raising syntax error:invalid syntax
ReplyDeleteif __name__ == '__main__':
ReplyDeleteN = int(input())
Out_put = []
for i in range(0,N):
ip = input().split()
if ip[0] == "print":
print(Out_put)
elif ip[0] == "insert":
Out_put.insert(int(ip[1]),int(ip[2]))
elif ip[0] == "remove":
Out_put.remove(int(ip[1]))
elif ip[0] == "pop":
Out_put.pop()
elif ip[0] == "append":
Out_put.append(int(ip[1]))
elif ip[0] == "sort":
Out_put.sort()
else:
Out_put.reverse()
this is correct code for python language