题解 | #表示数字#

表示数字

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)

全部评论

相关推荐

没有offer的呆呆:薪资有的时候也能说明一些问题,太少了活不活得下去是一方面,感觉学习也有限
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务