极度不优雅

配置文件恢复

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

import sys

list1 = []
dic = {
    # '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'
    }
while True:
    a = sys.stdin.readline().strip()
    if a == '':
        break
    list1.append(a)
for i in list1:
    j = i.split(' ')
    if len(j) < 1 or len(j) > 2:
        print('unkonwn command')
    if len(j) == 1:
        if j[0] == ['reset'][0][:len(j[0])]:
            print('reset what')
        else:
            print('unknown command')
    elif len(j) == 2:
        list2 = []
        for key, value in dic.items():
            m = key.split(' ')
            if j[0] == m[0][:len(j[0])] and j[1] == m[1][:len(j[1])]:
                list2.append(value)
        if len(list2) == 1:
            print(list2[0])
        else:
            print('unknown command')

全部评论
j[0] == ['reset'][0][:len(j[0])],还能这么写学到了👍
点赞 回复 分享
发布于 2021-11-28 15:34
不太优雅但直白
点赞 回复 分享
发布于 2021-03-09 11:01
不优雅但容易理解
点赞 回复 分享
发布于 2021-01-17 10:34

相关推荐

03-16 22:00
武汉大学 C++
幸福的小熊猫想要offer:我阿里投的 c++岗,面试官说自己是做 java 的,c++这辈子才有了
点赞 评论 收藏
分享
评论
3
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务