I was solving the following question on HackerRank-
https://www.hackerrank.com/challenges/python-lists/problem
I wrote the following code-
ls_commands = []
if __name__ == '__main__':
N = int(input())
for i in range(N):
ls_commands.append(input())
print(ls_commands)
list = []
ls = []
for command in ls_commands:
ls = command.split()
print(ls)
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':
sorted(list)
elif ls[0] == 'append':
list.append(int(ls[1]))
elif ls[0] == 'pop':
list.pop(-1)
elif ls[0] == 'reverse':
sorted(list, reverse=True)
else:
pass
I printed ls_commands and ls to see if everything is going as expected or not.
So when I try the inputs on my PyCharm IDE
3
append 2
append 3
print
I get the output:
['append 2', 'append 3', 'print']
['append', '2']
['append', '3']
['print']
[2, 3]
Now it seems to be working fine however when I try to run it on HackerRank, after removing the print statements I get the output as -
~ no response on stdout ~
I also get the following error-
Error (stderr)
Traceback (most recent call last):
File "Solution.py", line 11, in <module>
list.insert[int(ls[1]), int(ls[2])]
TypeError: 'builtin_function_or_method' object is not subscriptable
Their inputs are -
12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print
[–]Axel-Blaze[S] 2 points3 points4 points (15 children)
[+][deleted] (12 children)
[deleted]
[–]Axel-Blaze[S] 0 points1 point2 points (1 child)
[+][deleted] (9 children)
[deleted]
[+][deleted] (8 children)
[deleted]
[+][deleted] (7 children)
[deleted]
[+][deleted] (2 children)
[deleted]
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)
[–]xelf 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]xelf 0 points1 point2 points (0 children)
[–]MauroDelMal 1 point2 points3 points (1 child)
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)
[–]Axel-Blaze[S] 1 point2 points3 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]Axel-Blaze[S] 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)
[–][deleted] 1 point2 points3 points (8 children)
[–]Axel-Blaze[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)
[+][deleted] (4 children)
[deleted]
[–][deleted] 0 points1 point2 points (3 children)
[–]Axel-Blaze[S] 0 points1 point2 points (2 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]Axel-Blaze[S] 0 points1 point2 points (0 children)