Home > Back-end >  Hacker rank List Prepare
Hacker rank List Prepare

Time:06-17

This is my Code and it gives me this error message. Why?

if __name__ == '__main__':
    l=[]
    N = int(input())
    for z in range(N):
        x=input().split()
        if (x[0]=="insert"):
            l.insert(int(x[1]),int(x[2]))
        elif(x[0]=="print"):
            print(l)
        elif(x[0]=="remove"):
            l.remove(int(x[1]))
        elif (x[0]=="append"):
            l.append(x[1])
        elif(x[0]=="sort"):
            l.sort()
        elif(x[0])=="pop":
            l.pop()
        elif(x[0]=="reverse"):
            l.reverse()

Error Message - Traceback (most recent call last): File "/tmp/submission/20220617/03/45/hackerrank-3495035b4042c8bc0c55e799a2d2e687/code/Solution.py", line 15, in l.sort() TypeError: '<' not supported between instances of 'str' and 'int'

CodePudding user response:

You appended string value at x[0] == "append".

It should work when you change to l.append(int(x[1])

CodePudding user response:

I am sorry. Sample input and output is.

Sample Input:

12
insert 0 5
insert 1 10
insert 0 6
print
remove 6
append 9
append 1
sort
print
pop
reverse
print

Output :

[6, 5, 10]
[1, 5, 9, 10]
[9, 5, 1]
  • Related