Sort by

recency

|

2821 Discussions

|

  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        arr=[]
        
        for x in range(N):
            code = input().split()
            command = code[0]
            if len(code)>1:
                num = int(code[1])
            if len(code)>2:
                pos = int(code[2])
            
            match command:
                case "insert":
                    arr.insert(num, pos)
                case "print":
                    print(arr)
                case "remove":
                    arr.remove(num)
                case "append":
                    arr.append(num)
                case "sort":
                    arr.sort()
                case "pop":
                    arr.pop()
                case "reverse":
                    arr.reverse()
    
  • + 0 comments

    Hi guys, This is the solution I thought of. Here, the commands must be taken iteratively as input. for that, we can use input().split() after a for loop, where the split() command stores the command line entered as a list!

    if command is stored as list, for example, ['insert', '0'] command would mean insert operation with 0 value. so we assign the 0th position of the command line as operation. then we convert the value to integer as the value to be added to new list is a number!

    here is the code:

    Spoiler

    if __name__ == '__main__':
        N = int(input())
     
        a = list()
    
        for i in range(N):
            cmd = input().split()
            operation = cmd[0] 
            
            if operation=="insert":
                a.insert(int(cmd[1]), int(cmd[2]))
            elif operation=="remove":
                a.remove(int(cmd[1]))
            elif operation=="append":
                a.append(int(cmd[1]))
            elif operation=="sort":
                a.sort()
            elif operation=="pop":
                a.pop()
            elif operation=="reverse":
                a.reverse()
            elif operation=="print":
                print(a)
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        result = []
        for _ in range(N):
            command = input().split()
            if  "print" in command:
                print(result)
            elif "insert" in command:
                result.insert(int(command[1]),int(command[2]))
            elif "remove" in command:
                result.remove(int(command[1]))
            elif "append" in command:
                result.append(int(command[1]))
            else:
                list_action = getattr(result, command[0])
                list_action()
    
  • + 0 comments
    from operator import methodcaller
    
    class List(list):
        def print(self):
            print(self)
    	
    (...)
    
  • + 0 comments
    N = int(input())        
    arr = []
    for _ in range(N):
        command = input().split()
        match command[0]:
            case 'insert':
                arr.insert(int(command[1]), int(command[2]))
            case 'print':
                print(arr)
            case 'remove':
                arr.remove(int(command[1]))
            case 'append':
                arr.append(int(command[1]))
            case 'sort':
                arr.sort()
            case 'pop':
                arr.pop()
            case 'reverse':
                arr.reverse()