题解 | #表示数字#

表示数字

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

s = input()
# 两边扩展方便比较前后位置是否为数字
s = "#" + s + "#"
out = ""
# 输出结果从idx=1开始到idx=len(s)-2结束, 正好跳过首位#
for idx in range(1, len(s) - 1):
    ch = s[idx]
    if (not "0" <= s[idx - 1] <= "9") and "0" <= ch <= "9" and (not "0" <= s[idx + 1] <= "9"):
        out += "*" + ch + "*"
    elif (not "0" <= s[idx-1] <= "9") and "0" <= ch <= "9":
        out += "*" + ch
    elif "0" <= ch <= "9" and (not "0" <= s[idx + 1] <= "9"):
        out += ch + "*"
    else:
        out += ch
print(out)

全部评论

相关推荐

写不来代码的小黑:这么小的城市能有做it的公司也不容易
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

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