Sort by

recency

|

2842 Discussions

|

  • + 0 comments

    if name == 'main': N = int(input()) # Number of commands my_list = [] # Initialize empty list

    for _ in range(N):
        command = input().strip().split()
        operation = command[0]
    
        if operation == 'insert':
            i = int(command[1])
            e = int(command[2])
            my_list.insert(i, e)
        elif operation == 'print':
            print(my_list)
        elif operation == 'remove':
            e = int(command[1])
            my_list.remove(e)
        elif operation == 'append':
            e = int(command[1])
            my_list.append(e)
        elif operation == 'sort':
            my_list.sort()
        elif operation == 'pop':
            my_list.pop()
        elif operation == 'reverse':
            my_list.reverse()
    
  • + 0 comments

    if name == 'main': N = int(input()) # Number of commands my_list = [] # Initialize empty list

    for _ in range(N):
        command = input().strip().split()
        operation = command[0]
    
        if operation == 'insert':
            i = int(command[1])
            e = int(command[2])
            my_list.insert(i, e)
        elif operation == 'print':
            print(my_list)
        elif operation == 'remove':
            e = int(command[1])
            my_list.remove(e)
        elif operation == 'append':
            e = int(command[1])
            my_list.append(e)
        elif operation == 'sort':
            my_list.sort()
        elif operation == 'pop':
            my_list.pop()
        elif operation == 'reverse':
            my_list.reverse()
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input()) # number of commands
        mylist=[]
        for i in range(N):
            cmd=input().strip().split() #command
            if cmd[0]=="insert":
                mylist.insert(int(cmd[1]),int(cmd[2]))
            elif cmd[0]=="print":
                print(mylist)
            elif cmd[0]=="remove":
                mylist.remove(int(cmd[1]))
            elif cmd[0]=="append":
                mylist.append(int(cmd[1]))
            elif cmd[0]=="sort":
                mylist.sort()
            elif cmd[0]=="pop":
                mylist.pop()
            elif cmd[0]=="reverse":
                mylist.reverse()
    
  • + 0 comments

    What I implemented in this problem: list methods, string split, data type conversion

    Here's my code:

    if name == 'main': N = int(input())

    list_demo = []
    
    for i in range(N):
        ip = input()
    
        s = ip.split()
    
        if s[0]=='insert':
            index = int(s[1])
            list_demo.insert(index,int(s[2]))
    
        elif s[0]=='remove':
            list_demo.remove(int(s[1]))
        elif s[0]=='append':
            list_demo.append(int(s[1]))
        elif s[0]=='sort':
            list_demo.sort()
        elif s[0]=='pop':
            list_demo.pop()
        elif s[0]=='reverse':
            list_demo.reverse()
        else:
            print(list_demo)
    
  • + 0 comments

    Python Solution
    This solution uses switch statements.

    if __name__ == '__main__':
        N = int(input())
        l = []
        for _ in range(N):
            command = input().split(' ')
            match command[0]:
                case 'insert':
                    l.insert(int(command[1]), int(command[2]))
                case 'print':
                    print(l)
                case 'remove':
                    l.remove(int(command[1]))
                case 'append':
                    l.append(int(command[1]))
                case 'sort':
                    l.sort()
                case 'pop':
                    l.pop()
                case 'reverse':
                    l.reverse()