← 返回题库
初级

配置文件恢复

未完成
初级参考 完整示例代码供参考,建议自己理解后重新输入
def solve(data):
    CMDS = {
        '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',
    }
    for line in str(data).strip().split(','):
        parts = line.strip().split()
        matches = []
        if len(parts) == 1:
            for k in CMDS:
                ks = k.split()
                if len(ks) == 1 and ks[0].startswith(parts[0]):
                    matches.append(k)
        elif len(parts) == 2:
            for k in CMDS:
                ks = k.split()
                if len(ks) == 2 and ks[0].startswith(parts[0]) and ks[1].startswith(parts[1]):
                    matches.append(k)
        if len(matches) == 1:
            print(CMDS[matches[0]])
        else:
            print('unknown command')

示例

输入
solve('reset,reset board,board a,reboot backplane,board,abort')
期望输出
reset what
board fault
where to add
impossible
unknown command
unknown command
Python 代码 🔒 登录后使用
🔒

登录后即可练习

注册免费账号,在浏览器中直接运行 Python 代码