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.
defexecute_command(command:list[str],current_list:list[int])->None:"""Execute a command on a given list"""matchcommand[0]:case"insert":current_list.insert(int(command[1]),int(command[2]))case"print":print(current_list)case"remove":current_list.remove(int(command[1]))case"append":current_list.append(int(command[1]))case"sort":current_list.sort()case"pop":current_list.pop()case"reverse":current_list.reverse()case_:raiseValueError("Unknown command: ",command[0])defrun_commands(commands:list[str])->None:"""Run a sequence of commands on a list"""current_list=[]forlineincommands:command=line.strip().split()ifcommand:execute_command(command,current_list)if__name__=="__main__":n=int(input())commands=[input()for_inrange(n)]run_commands(commands)
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 →