题解 | #表示数字#
表示数字
https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6
import sys def do(s): l_result = [] for i in range(len(s)): # 循环 # 首位判断是否为数字,首在前面加* if i ==0 : if ord(s[i])>=ord('0') and ord(s[i])<=ord('9'): l_result.append('*') if ord(s[i])>=ord('0') and ord(s[i])<=ord('9') and (ord(s[i+1]) not in [ord(str(i))for i in range(10)]): l_result.append(s[i]) l_result.append('*') else: l_result.append(s[i]) # 中间的数据为数字并且前一个是字符加* elif i>0: if (ord(s[i])>=ord('0') and ord(s[i])<=ord('9')) : if ord(s[i-1]) not in [ord(str(i))for i in range(10)]: l_result.append('*') l_result.append(s[i]) if (i<len(s)-1) and ord(s[i+1]) not in [ord(str(i))for i in range(10)]: l_result.append('*') else: l_result.append(s[i]) # 尾巴最后是数字加* if i == (len(s)-1) and ord(s[i])>=ord('0') and ord(s[i])<=ord('9'): l_result.append('*') # 返回list转字符串 return ''.join(l_result) if __name__ == '__main__': s = sys.stdin.readline().strip() r = do(s) print(r)