Sort by

recency

|

2870 Discussions

|

  • + 0 comments
    def execute_command(command: list[str], current_list: list[int]) -> None:
        """Execute a command on a given list"""
        
        match command[0]:
            case "insert":
                current_list.insert(int(command[1]), int(command[2]))
            case "print":
                print(current_list)
            case "remove":
                current_list.remove(int(command[1]))
            case "append":
                current_list.append(int(command[1]))
            case "sort":
                current_list.sort()
            case "pop":
                current_list.pop()
            case "reverse":
                current_list.reverse()
            case _:
                raise ValueError("Unknown command: ", command[0])
    
    def run_commands(commands: list[str]) -> None:
        """Run a sequence of commands on a list"""
        
        current_list = []
        for line in commands:
            command = line.strip().split()
            if command:
                execute_command(command, current_list)
        
    if __name__ == "__main__":
        n = int(input())
        commands = [input() for _ in range(n)]
        run_commands(commands)
    
  • + 1 comment

    I didn't understand this, I understood list and its methods but didn't understand the question. Input N = N no.of commands, but how are the commands chosen?

  • + 0 comments

    if name == 'main': l=[] l2=[] N=int(input()) for _ in range(N): l.append(input().split()) #print(l) for i in l: match i[0]: case "append": a,b=i l2.append(int(b)) case "insert": a,b,c=i l2.insert(int(b),int(c)) case "remove": a,b=i l2.remove(int(b)) case "pop": l2.pop() case "reverse": l2.reverse() case "sort": l2.sort() case "print": print(l2)

  • + 0 comments

    if name == 'main': N = int(input()) result=[] for i in range(N): commands= input().split()

        match commands[0]:
            case "insert":
                result.insert(int(commands[1]),int(commands[2]))
            case "print":
                print(result)
            case "remove":
                result.remove(int(commands[1]))
            case "append":
                result.append(int(commands[1]))
            case "sort":
                result.sort()
            case "pop":
                result.pop()
            case "reverse":
                result.reverse()
    
  • + 0 comments
    if __name__ == '__main__':
        N = int(input())
        res = []
        for i in range(N):
            row = input().split()
            
            match row[0]:
                case "append":
                    res.append(int(row[1]))
                case "reverse":
                    res.reverse()
                case "sort":
                    res = sorted(res)
                case "insert":
                    res.insert(int(row[1]), int(row[2]))
                case "print":
                    print(res)
                case "remove":
                    res.remove(int(row[1]))
                case "pop":
                    res.pop()