In this HackerRank Lists problem solution, we have given a list and we need to perform the insert, print, remove, append, sort, pop and reverse operations on the lists.
HackerRank Lists in python problem solution.
# Enter your code here. Read input from STDIN. Print output to STDOUT
N = int(raw_input())
L = []
for i in range(N):
command = raw_input().split()
#print command
if command[0] == 'insert':
L.insert(int(command[1]), int(command[2]))
elif command[0] == 'append':
L.append(int(command[1]))
elif command[0] == 'remove':
L.remove(int(command[1]))
elif command[0] == 'pop':
L.pop()
elif command[0] == 'sort':
L.sort()
elif command[0] == 'reverse':
L.reverse()
elif command[0] == 'index':
print L.index(command[1])
elif command[0] == 'count':
print L.count(command[1])
else:
print L
Second solution
class mylist(list):
def print(self):
print(self)
L = mylist()
for _ in range(int(input())):
s = input().rstrip().split(" ")
cmd = "L."+s[0]+"("+",".join(s[1:])+")"
eval(cmd)
2 Comments
can you please explain each line.thanks.
ReplyDeletesorry beginner here.
ReplyDelete