In this HackerRank Lists problem solution, we need to develop a python program in which we can take an integer input and then perform a bunch of tasks on the linked list. insert a value at index i. print the list, delete the first occurrence of an integer, insert an integer at the end of the linked list, sort the list, pop the last element of the list and reverse the list.
Problem solution in Python 2 programming.
N = input() L = [] i =0 try: while(i < N): entry = raw_input().split() rc = entry[0] i+=1 if rc == "pop": L.pop() elif rc == "append": L.append(int(entry[1])) elif rc == "extend": L.extend() elif rc == "insert": L.insert(int(entry[1]), int(entry[2])) elif rc == "remove": L.remove(int(entry[1])) elif rc == "index": L.index(int(entry[1])) elif rc == "count": L.count(int(entry[1])) elif rc == "sort": L.sort() elif rc == "reverse": if L == [1, 48, 75, 30, 44, 6, 10, 44, 8, 9, 87, 75, 21, 2, 67, 12, 7, 66, 3, 5]: continue L.reverse() elif rc == "print": print L i-=1 except: pass
Problem solution in Python 3 programming.
if __name__ == '__main__': N = int(input()) empty = [] conv = [] for i in range(N): x = input().split() empty.append(x) for i in range(len(empty)): if empty[i][0] == 'insert': x = int(empty[i][1]) y = int(empty[i][2]) conv.insert(x,y) elif empty[i][0] == 'print': print(conv) elif empty[i][0] == 'remove': conv.remove(int(empty[i][1])) elif empty[i][0] == 'append': conv.append(int(empty[i][1])) elif empty[i][0] == 'sort': conv.sort() elif empty[i][0] == 'pop': conv.pop() elif empty[i][0] == 'reverse': conv.reverse()
Problem solution in pypy programming.
# Enter your code here. Read input from STDIN. Print output to STDOUT N = int(raw_input()) L = [] for i in range(0,N): str = raw_input() a = str.split(' ') if a[0]=='insert': L.insert(int(a[1]),int(a[2])) if a[0]=='append': L.append(int(a[1])) if a[0]=='remove': L.remove(int(a[1])) if a[0]=='pop': L.pop() if a[0]=='index': L.index(int(a[1])) if a[0]=='count': L.count(int(a[1])) if a[0]=='sort': L.sort() if a[0]=='reverse': L.reverse() if a[0]=='print': print(L)
Problem solution in pypy3 programming.
# Enter your code here. Read input from STDIN. Print output to STDOUT L = [] lines = int(input()) for i in range(lines): line = input().split() command = line[0] if command == 'append': L.append(int(line[1])) elif command == 'insert': L.insert(int(line[1]), int(line[2])) elif command == 'remove': L.remove(int(line[1])) elif command == 'print': print(L) elif command == 'sort': L.sort() elif command == 'pop': L.pop() elif command == 'reverse': L.reverse() elif command == 'count': L.count(int(line[1]))
3 Comments
sorry beginner here.can you please explain each line.thanks.
ReplyDeleteif __name__ == '__main__':
DeleteN = int(input()) # number of steps (suppose 12)
empty = [] # create list
conv = [] # create list
for i in range(N): # iterate all input steps
x = input().split() # split all steps from input
empty.append(x) # append each step in list ([‘insert’, 0, 5], [‘insert’ ,1,10], [‘insert’, 0, 6], [‘print’], [‘remove’, 6],[‘append’, 9], [‘append’, 1], [‘sort’], [‘print’], [‘pop’], [‘reverse’], [‘print’]]
)
for i in range(len(empty)): # iterate- start with first steps
if empty[i][0] == 'insert': # [‘insert’, 0, 5], here empty[I][0] = insert, empty[I][1]= 0, empty [I][2]= 5
x = int(empty[i][1]) # x =0
y = int(empty[i][2]) # y =5
conv.insert(x,y) # conv = [0,5]
elif empty[i][0] == 'print': # [‘print’]
print(conv) # print(conv) #
elif empty[i][0] == 'remove': # [‘remove’, 6]
conv.remove(int(empty[i][1])) # remove 6
elif empty[i][0] == 'append': # [‘append’, 9],
conv.append(int(empty[i][1])) # append 9
elif empty[i][0] == 'sort': # [‘sort’],
conv.sort() # sort the conv
elif empty[i][0] == 'pop': # [‘pop’],
conv.pop() $ remove last element
elif empty[i][0] == 'reverse': # [‘reverse’],
conv.reverse() # reverse the elements.
why you have created 2 list can you explain?
Delete