2
\$\begingroup\$

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)
\$\endgroup\$

1 Answer 1

3
\$\begingroup\$

You have the right idea. There are a few details that could be tightened up.

In the main driver:

  • command is not really what I would consider a command; it's just a useless counter. By convention, unused variables should be named _.
  • temp is always a poor variable name. In this case, command would be a better name than temp. However, I don't think you need to give it a name at all — just write input().split(). The .strip() is superfluous, since .split() will discard leading and trailing whitespace anyway. You don't need to convert strings to strings using str().

Naming and parameter-passing for the commdr() function could be improved as well. Here's how I would write it:

def execute(lst, cmd, *args):
    if cmd == 'insert':
        lst.insert(int(args[0]), int(args[1]))
    elif cmd == 'print':
        print(lst)
    elif cmd == 'remove':
        lst.remove(int(args[0]))
    elif cmd == 'append':
        lst.append(int(args[0]))
    elif cmd == 'sort':
        lst.sort()
    elif cmd == 'reverse':
        lst.reverse()
    elif cmd == 'pop':
        lst.pop()
    else: 
        print("Command not recognized!")

lst = []
for _ in range(int(input())):
    execute(lst, *input().split())

Bonus golfed solution

If you have absolutely no concern for validation or security, you could take advantage of the fact that most of the commands look just like existing methods in Python's list.

class ListInterp(list):
    def print(self):
        print(self)

    def execute(self, cmd, *args):
        getattr(self, cmd)(*map(int, args))

lst = ListInterp()
for _ in range(int(input())):
    lst.execute(*input().split())

I'm not saying that this is good code… I just thought I would mention that there is a quick-and-dirty solution.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.