Sort by

recency

|

2932 Discussions

|

  • + 0 comments

    done it using match case ot Switch statement you can call it but i have a question i was not able to change the value of cmd list like this can you guys help me with that why it cannot be done like that

    cmd[1]=int(cmd[1])

    if name == 'main':

    N = int(input())

    new_list=[]

    for _ in range(N) :    
        cmd=input().split()
        match cmd[0]:
            case "append":
               new_list.append(int(cmd[1])) 
            case "insert":
                new_list.insert(int(cmd[1]),int(cmd[2]))
            case "print":
                print(new_list)
            case "remove":
                new_list.remove(int(cmd[1]))
            case "sort":
                new_list.sort()
            case "pop":
                if new_list!=[]:
                    new_list.pop()
            case "reverse":
                new_list.reverse()
    
  • + 0 comments

    done it using match case or Switch statement you can call it but i have a question i was not able to change the value of cmd list like this

    cmd[1]=int(cmd[1])

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

    for _ in range(N) :    
        cmd=input().split()
        match cmd[0]:
            case "append":
               new_list.append(int(cmd[1])) 
            case "insert":
                new_list.insert(int(cmd[1]),int(cmd[2]))
            case "print":
                print(new_list)
            case "remove":
                new_list.remove(int(cmd[1]))
            case "sort":
                new_list.sort()
            case "pop":
                if new_list!=[]:
                    new_list.pop()
            case "reverse":
                new_list.reverse()
    
  • + 0 comments
    N = int(input()) 
    arr = []
    
    for _ in range(N):
    		cmd = list(input().split())
    
    		if cmd[0] == "insert":
    				arr.insert(int(cmd[1]),int(cmd[2]))
    		elif cmd[0] == "print":
    				print(arr)
    		elif cmd[0] == "remove":
    				arr.remove(int(cmd[1]))
    		elif cmd[0] == "append":
    				arr.append(int(cmd[1]))
    		elif cmd[0] == "sort":
    				arr.sort()
    		elif cmd[0] == "pop":
    				arr.pop()
    		elif cmd[0] == "reverse":
    				arr.reverse()
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        
        arr = []
        while (N != 0):
            command = input().split()# ("command", "position(OPT)", "value(OPT)")
            
            instr = command[0] #0 -> command, 1 -> position, 2 -> value
            
            if instr == "insert": #Only command that takes 2 inputs
                pos, val = int(command[1]), int(command[2])
                arr.insert(pos, val)
            
            elif instr == "append" :
                val = int(command[1])
                arr.append(val)
            
            elif instr == "remove":
                val = int(command[1])
                arr.remove(val) #when using arr.remove() and there is duplicates it is only the first instance that is removed
            
            elif instr == "sort":
                arr.sort()
            elif instr ==  "pop":
                arr.pop()
            elif instr == "reverse":
                arr.reverse()
            
            else:
                print(arr)
            
            N-= 1
    
  • + 0 comments
    if __name__ == '__main__':
        list=[]
        n = int(input())
        for i in range(n):
            ip=input().split()
            cmd=ip[0]
            
            if cmd == "insert":
                list.insert(int(ip[1]),int(ip[2]))
            elif cmd=="print":
                print(list)
            elif cmd=="remove":
                list.remove(int(ip[1]))
            elif cmd == "append":
                list.append(int(ip[1]))
            elif cmd == "sort":
                list.sort()
            elif cmd == "pop":
                list.pop()
            else:
                list.reverse()