I completed the following exercise on HackerRank. My program is fed a list of commands, such as:
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
Where the first line of the input is the number of commands, the rest of the lines commands and parameters.
Here is my code, nothing too fancy; any and all comments welcome!
def commdr(lst, instruct):
if instruct[0] == 'insert':
lst.insert(int(instruct[1]), int(instruct[2]))
elif instruct[0] == 'print':
print(lst)
elif instruct[0] == 'remove':
lst.remove(int(instruct[1]))
elif instruct[0] == 'append':
lst.append(int(instruct[1]))
elif instruct[0] == 'sort':
lst.sort()
elif instruct[0] == 'reverse':
lst.reverse()
elif instruct[0] == 'pop':
lst.pop()
else:
print("Command not recognized!")
n = int(input())
lst = []
for command in range(0,n):
temp = [str(i) for i in input().strip().split()]
commdr(lst, temp)