Sort by

recency

|

2904 Discussions

|

  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
    
        
        def action(com_list, arr):
            if com_list[0] == 'print':
                print(arr)
            elif com_list[0] == 'pop' and len(arr) > 0:
                arr.pop()
            elif com_list[0] == 'reverse' and len(arr) > 0:
                arr.reverse()
            elif com_list[0] == 'insert':
                i, e = int(com_list[1]), int(com_list[2])
                arr.insert(i, e)
            elif com_list[0] == 'sort':
                arr.sort()
            elif com_list[0] == 'remove':
                try:
                    arr.remove(int(com_list[1]))
                except ValueError:
                    pass
            elif com_list[0] == 'append':
                e = int(com_list[1])
                arr.append(e)
                    
        my_list = []
        
        for i in range(N):
            command = input().split()
            action(command, my_list)
    
  • + 0 comments
    if __name__ == "__main__":
        N = int(input())
    
        operations: list[tuple[str, list[int]]] = [("", [])]
    
        operations = [
            (command, list(map(int, arguments)))
            for command, *arguments in (input().split() for _ in range(N))
        ]
    
        my_list: list[int] = []
    
        for command, arguments in operations:
            match command:
                case "insert":
                    my_list.insert(arguments[0], arguments[1])
    
                case "print":
                    print(my_list)
    
                case "remove":
                    my_list.remove(arguments[0])
    
                case "append":
                    my_list.append(arguments[0])
    
                case "sort":
                    my_list.sort()
    
                case "pop":
                    my_list.pop()
    
                case "reverse":
                    my_list.reverse()
    
                case _:
                    raise ValueError(f"Unknown command: {command}")
    
  • + 1 comment
    if __name__ == '__main__':
        N = int(input())
        lst = []
    
        for _ in range(N):
            parts = input().split()
            cmd = parts[0]
            args = list(map(int, parts[1:]))  # convert remaining parts to integers
    
            if cmd == "print":
                print(lst)
            else:
                getattr(lst, cmd)(*args)  # *args unpacks the list into separate arguments
    
  • + 0 comments

    For Python3 Platform

    N = int(input())
    lst = []
    
    for _ in range(N):
        cmd = input().split()
        
        if(cmd[0] == "insert"):
            lst.insert(int(cmd[1]), int(cmd[2]))
        elif(cmd[0] == "print"):
            print(lst)
        elif(cmd[0] == "remove"):
            lst.remove(int(cmd[1]))
        elif(cmd[0] == "append"):
            lst.append(int(cmd[1]))
        elif(cmd[0] == "pop"):
            lst.pop()
        elif(cmd[0] == "reverse"):
            lst.reverse()
        elif(cmd[0] == "sort"):
            lst.sort()
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        lst = []
        inputs = [
            [line[0], *map(int,line[1:])] 
            for line in (input().split() for _ in range(N))
        ]
    
        for command_input in inputs:
            try:
                getattr(lst, command_input[0])(*command_input[1:])
            except AttributeError:
                getattr(__builtins__, command_input[0])(*[lst,*command_input[1:]])