We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
if__name__=='__main__':#N signafies how many lines there will be following the first lineN=int(input())#initalizing a list variable called ll=[]#this loop goes from 0 to N-1for_inrange(N):#take the input, put the first input in command variable,#put second input in list called argumentscommand,*arguments=input().split()#here cast the elements in list from string to intarguments=list(map(int,arguments))try:#*arguments, why it has a * before arguments:#geeksforgeeks: "It is used to pass a variable number of arguments to a#function"#you can see that arguments is a list, thus we need to pass the individual#elements, not the whole list at once#the * allows all the individual elements to be passed#comment I found on hackerrank to clearify confusion#on how getattr()() works:#"Like for the case (insert 0 5) it is like getattr(l,insert)(0,5). #We are passing 0 and 5 to insert so that it will add 5 #at the index 0 in the list."getattr(l,command)(*arguments)#this is if the try block fails#the one condition it will fail is the print statement#print is not a command you can do on a list#commands you can do on a list are insert, remove, append...etcs#thus it will throw an attribute error saying this can not be doneexceptAttributeError:#here we are taking the command and evaluating that on the list l#thus the only condition will be print + l which when evaluated#will print the listeval(command+"(l)")
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Lists
You are viewing a single comment's thread. Return to all comments →