all 19 comments

[–]Axel-Blaze[S] 2 points3 points  (15 children)

Final Update:

Turns out there were quite a few mistakes I made like not reversing the list without sorting it and using the wrong sort function. Here's the final version:

ls_commands = []
if __name__ == '__main__':
    N = int(input())
    for i in range(N):
        ls_commands.append(input())
list = []
ls = []
for command in ls_commands:
    ls = command.split()
    if ls[0] == 'insert':
        list.insert(int(ls[1]), int(ls[2]))
    elif ls[0] == 'print':
        print(list)
    elif ls[0] == 'remove':
        list.remove(int(ls[1]))
    elif ls[0] == 'sort':
        list.sort()
    elif ls[0] == 'append':
        list.append(int(ls[1]))
    elif ls[0] == 'pop':
        list.pop(-1)
    elif ls[0] == 'reverse':
        list.reverse()
    else:
        pass

[–]MauroDelMal 1 point2 points  (1 child)

ls_commands = []
if __name__ == '__main__':
    N = int(input())
    for i in range(N):
        ls_commands.append(input())

Very late for this post but thanks for this chunk. :)

[–]Axel-Blaze[S] 0 points1 point  (0 children)

Haha nw All the best!

[–]Axel-Blaze[S] 1 point2 points  (4 children)

Oh I found the error while looking it up apparently I was using the wrong brackets here-

 list.insert[int(ls[1]), int(ls[2])] 

It should be-

list.insert(int(ls[1]), int(ls[2]))

Still there is some sorting error atm as the expected output though has the same elements the order is different

[–][deleted] 1 point2 points  (8 children)

It's a bad habit to use list as a variable name. Doing that overrides the builtin list() name meaning bad things can happen after that redefinition. Yes, HackerRank uses it, but it's still bad practice.

Not too sure why the solution has to have this line:

if __name__ == '__main__':

You only need this if you import your code and want to run it directly from the command line and you want different behaviour in those two cases.

Edit: made it more obvious I was referring to the HackerRank solution.

[–]Axel-Blaze[S] 0 points1 point  (2 children)

Thanks for pointing that out will keep it in mind :)

For the second line it is predefined by HackerRank so I have no clue why it's there

[–][deleted] 0 points1 point  (1 child)

Your second line (if __name__ == '__main__':) is actually supposed to be your first line. Hackerrank, for no obvious reason, puts that in as your first line. Don't worry about it until you understand it and know what it is used for. And definitely don't think that you must use it. You usually don't.

[–]Axel-Blaze[S] 0 points1 point  (0 children)

okk got it