题解 | #表示数字#

表示数字

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

全部评论

相关推荐

09-09 21:23
门头沟学院 Java
程序员牛肉:小牛肉来也! 主要就是没有实习经历。因为你的投递方向肯定是中小厂。在小厂中,很少会有公司愿意花钱培养你。因此会更加青睐有实习的同学。再加上你的学历比较差一点,所以找不到是正常的。 跟简历项目啥的已经没有大关系了,就是差一份实习。秋招和日常实习一起投递吧。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务