题解 | #表示数字#

表示数字

https://www.nowcoder.com/practice/637062df51674de8ba464e792d1a0ac6

import sys


# from numpy.core.defchararray import isdigit
# for line in sys.stdin:
#     a = line.split()
#     print(int(a[0]) + int(a[1]))


# 判断该字符是否为数字,如果为数字则给前面加上一个星号
# 如果该数字前面的字符为数字,则该数字前面不加入星号
while True:
    try:
        str = input()
        pre_single_str = ""
        res = str
        tmp_num = 0
        for i, c in enumerate(str):
            # 当前字符是数字
            if c.isdigit():
                # 当前字符是数字,上一个字符是数字,那么忽略
                if pre_single_str.isdigit():
                    pass
                # 当前字符是数字,上一个字符不是数字,加星号
                else:
                    # 如果是手首字符,如果是中间字符两种情况
                    if pre_single_str == "":
                        res = "*" + res
                        tmp_num = tmp_num + 1
                    else:
                        res = res[: i + tmp_num] + "*" + res[i + tmp_num :]
                        tmp_num = tmp_num + 1
                 # 如果是最后一位那么在末尾增加*
                if i == len(str) - 1:
                    res = res + "*"
            # 如果当前字符不是数字
            if c.isdigit() != True:
                # 前面字符是数字,则在其前面加上*
                if pre_single_str.isdigit():
                    res = res[: i + tmp_num] + "*" + res[i + tmp_num :]
                    tmp_num = tmp_num + 1  
            pre_single_str = c
        print(res)
    except:
        break

全部评论

相关推荐

05-09 13:22
门头沟学院 Java
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客企业服务