Sort by

recency

|

2802 Discussions

|

  • + 0 comments

    if name == 'main':

    N = int(input())
    
    def selector(N):
        lst = []
        commands = {
            'insert': lambda cmd: lst.insert(int(cmd[1]), int(cmd[2])),
            'print': lambda cmd: print(lst),
            'remove': lambda cmd: lst.remove(int(cmd[1])),
            'append': lambda cmd: lst.append(int(cmd[1])),
            'sort': lambda cmd: lst.sort(),
            'pop': lambda cmd: lst.pop(),
            'reverse': lambda cmd: lst.reverse()
        }
    
        for _ in range(N):
            cmd = input().lower().split(' ')
            commands[cmd[0]](cmd)
    
    selector(N)
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        lst=[]
        commands=[input().split(" ") for i in range(N)]
    
        def insert(args):
            lst.insert(args[0],args[1])
    
        def remove(args):
            lst.remove(args[0])
    
        def append(args):
            lst.append(args[0])
    
        def sort():
            lst.sort()
    
        def pop():
            lst.pop()
    
        def reverse():
            lst.reverse()
    
        def display():
            print(lst)
    
        operations={
            'insert': insert,
            'print':display,
            'remove':remove,
            'append':append,
            'sort':sort,
            'pop':pop,
            'reverse':reverse,
    
        }
    
        for command in commands:
            action=command[0]
            args =[int(i) for i in command[1:]]
            if action in operations.keys():
                if len(args)==0:
                    operations[action]()
                else:
                    operations[action](args)
    
  • + 0 comments

    here taking the user_input - insert 1 5 and coverting this to ['insert', '1', '5'] using split() and hence using indexing

    if name == 'main': N = int(input()) my_list = []

    for iterations in range(N):
        user_input = input().split()
    
        if user_input[0] == 'insert':
            my_list.insert(int(user_input[1]), int(user_input[2]))
        elif user_input[0] == 'print':
            print(my_list)
        elif user_input[0] == 'remove':
            my_list.remove(int(user_input[1]))
        elif user_input[0] == 'append':
            my_list.append(int(user_input[1]))
        elif user_input[0] == 'sort':
            my_list.sort()
        elif user_input[0] == 'pop':
            my_list.pop()
        elif user_input[0] == 'reverse':
            my_list.reverse()
    
  • + 1 comment
    if __name__ == '__main__':
        N = int(input())
        my_list = []
        for _ in range(N):
            command, *values = input().split()
            params = list(map(int, values))
            if command == "print":
                print(my_list)
            else:
                getattr(my_list, command)(*params)
    
  • + 0 comments
    if __name__ == '__main__':
        l = []
        for _ in range(int(input())):
            cmd,args = (lambda a: [a[0],a[1:]])(input().split())
            if cmd=="print":
                print(l)
            else:
                exec(f"l.{cmd}({", ".join(args)})")