Skip to content

Instantly share code, notes, and snippets.

@hyukkwonepic
Created June 3, 2016 08:47
Show Gist options
  • Save hyukkwonepic/abbef8036980e4ba2bdafe7dffc30d57 to your computer and use it in GitHub Desktop.
Save hyukkwonepic/abbef8036980e4ba2bdafe7dffc30d57 to your computer and use it in GitHub Desktop.
list and its methods - hackerank

#list and its methods [https://www.hackerrank.com/challenges/python-lists]

sample input

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

sample output

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

code

n = int(input())
samplelist = []

for times in range(n):
    q = input()
    qlist = q.split(" ")

    if len(qlist) == 3:
        command, x, y = qlist
        x = int(x)
        y = int(y)
        if command == "insert":
            samplelist.insert(x,y)
    if len(qlist) == 2:
        command, x = qlist
        x = int(x)
        if command == "remove":
            samplelist.remove(x)
        elif command == "append":
            samplelist.append(x)
    if len(qlist) == 1:
        command = qlist[0]
        if command == "print":
            print(samplelist)
        elif command == "pop":
            samplelist.pop()
        elif command == "reverse":
            samplelist.reverse()
        elif command == "sort":
            samplelist.sort()
        
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment