题解 | #配置文件恢复#

配置文件恢复

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

#使用正则表达式匹配,思路非常简单,命令分单字符串、双字符串和其他字符串,分别考虑就行

import re
min_zx = {"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",}

def is_ok(strr):
    b, outt, flagg, m_l =None, "", 0, strr.split(" ") #定义输出的内容、匹配多个的标签、输入命令转化为列表
    if len(m_l) == 1:
        b = re.compile("^{}[a-z]*$".format(strr))
    elif len(m_l) == 2:
        b = re.compile(r"^{}[a-z]*\s{}[a-z]*$".format(m_l[0],m_l[1]))
    if b:
        for i in min_zx.keys():
            if b.match(i):
                outt = min_zx[i]
                flagg += 1
        if flagg == 1:
            return outt
        else:
            return "unknown command"
    return "unknown command"
while True:
    try:
        a = input()
    except:
        break
    else:
        print(is_ok(a))
全部评论

相关推荐

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