题解 | #配置文件恢复#

配置文件恢复

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

python3代码如下:

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

def f(x):
    #先判断一下数组的字符串数量,三个以上的命令就不用考虑了
    le1=len(x)
    if le1<=2: 
        condition=0 #条件1
        flag=True #用于判断是否有重复匹配结果
        for i in range(le1):
            s=x[i]
            flag=True
            for j in range(len(command_arr)):
                c=command_arr[j].split(' ')
                if le1==len(c):
                    cc=c[i][:len(s)]
                    if s==cc and flag: 
                        condition=condition+1
                        flag=False
                        if condition==le1: return exe_arr[j]
    return 'unknown command'

for line in sys.stdin:
    if line != "\n":
        com=line.strip('\n').split(' ')
        print(f(com))
全部评论

相关推荐

不愿透露姓名的神秘牛友
2025-11-27 10:04
点赞 评论 收藏
分享
评论
1
1
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务