题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import sys def find_key(l): command = { "reset": "reset what", "reset board": "board fault", "board add": "where to add", "board delete": "no board at all", "reboot backplane": "impossible", "backplane abort": "install first", "he he":"unknown command" } keywords = [] # 将每个关键字以列表形式存入列表中 for i in command.keys(): if i not in keywords: keywords.append(i.split()) execute = "" # 用来存储查找后的执行结果 if len(l) == 1: if l[0] == keywords[0][0][: len(l[0])]: execute = command["reset"] else: execute = "unknown command" elif len(l) == 2: flag = 0 #用于判断是否有两个及以上的匹配命令 for keyword in keywords[1:]: if l[0] == keyword[0][: len(l[0])]: if l[1] == keyword[1][: len(l[1])]: # 对l两个字符串和keyword的两个字串分别比较 if flag == 0: execute = ' '.join(keyword) execute = command[execute] flag = 1 else: execute = "unknown command" break else: execute = "unknown command" return execute while True: try: l = list(input().split()) print(find_key(l)) except: break