题解 | #配置文件恢复#
配置文件恢复
https://www.nowcoder.com/practice/ca6ac6ef9538419abf6f883f7d6f6ee5
import sys #判断s字符串是否是以a字符串开始 def is_sucessed(a,s): if len(a) > len(s): return False for i in range(len(a)): if a[i] != s[i]: return False return True code = [['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 sys.stdin: a = line.strip().split(' ') if len(a) == 1: if is_sucessed(a[0],code[0][0]): print(code[0][1]) else: print('unknown command') else: sucess = 0 p = 'unknown command' for i in range(1,6): t = code[i][0].split(' ') if is_sucessed(a[0],t[0]) and is_sucessed(a[1],t[1]): sucess += 1 p = code[i][1] #如果匹配成功只有一次,则p会被改变为正确的输出。当被改变2次及以上,sucess大于1,此时p的值不会输出了,被改变成什么就不重要了 if sucess == 1: print(p) else: print('unknown command')
原始一点点的解法 文章被收录于专栏
尽量不借助面向对象的思想,自己去实习具体过程