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.
I went on a similar route, and to avoid the if/elif's, you can use wildcards. Also, you can check out what methods the list has, using dir() and then, callable()
Example:
arr = list()
tests = input()
for test in range(int(tests)):
test = input()
instruction = test.split(" ")
if instruction[0] in dir(arr) and callable(getattr(arr, instruction[0])):
args = instruction[1:]
args = list(map(int, args)) # str -> int
f = getattr(arr, instruction[0]) # get the callable method
f(*args) # and send it *whatever* argument you have
else:
if instruction[0] == "print":
print(arr)
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 →
I went on a similar route, and to avoid the if/elif's, you can use wildcards. Also, you can check out what methods the list has, using dir() and then, callable()
Example: