题解 | #配置文件恢复#

配置文件恢复

https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5

while 1:

    try:
        command = input()

        CandA = [['reset', 'reset board', 'board add', 'board delete', 'reboot backplane', 'backplane abort'], 
        ['reset what', 'board fault', 'where to add', 'no board at all', 'impossible', 'install first']]


        if ' ' not in command and command in CandA[0][0]:
            print(CandA[1][0])
        elif ' ' not in command:
            print('unknown command')
        elif ' ' in command:
            blank_index = command.index(' ')
            first_key = command[0:blank_index]
            second_key = command[blank_index + 1:len(command)]

            first_counter = 0
            second_counter = 0
            command_index = ''
            first_commander = []

            for i in range(1, len(CandA[0])):
                blank_index = CandA[0][i].index(' ')
                first_command = CandA[0][i][0:blank_index]
                if first_key == first_command[0:len(first_key)]:
                    first_counter += 1
                    first_commander.append(CandA[0][i])
            
            if first_counter:
                for i in range(len(first_commander)):
                    blank_index = first_commander[i].index(' ')
                    second_command = first_commander[i][blank_index + 1:len(first_commander[i])]
                    if second_key == second_command[0:len(second_key)]:
                        second_counter += 1
                        command_index = first_commander[i]
                        if second_counter >= 2:
                            break

                if second_counter == 1:
                    print(CandA[1][CandA[0].index(command_index)])
                else:
                    print('unknown command')

            else:
                print('unknown command')
        else:
            print('unknown command')

    except:
        break

全部评论

相关推荐

点赞 收藏 评论
分享
牛客网
牛客企业服务