首页 > 解决方案 > 如何将 a2c3d 转换为 accfd?

问题描述

在这里,我们只需要将写入前一个字符 ascii 值的 num 添加。

我试过了

if __name__ == '__main__':
    n = input()
    list1 = list(n)
    for i in list1:
        if list1[i] is not chr:
            list1[i] = list1[i-1] + list1[i]

    print(list(n))

标签: python

解决方案


这种方法的优点是不使用列表。它将前一个字符存储在prev变量中以在数字的情况下使用。

text = 'a2c3d'
result = ''
prev = None
for ch in text:
    if ch.isdigit() and prev:
        result += chr(int(ch) + ord(prev))
    else:
        result += ch
        prev = ch
print(result)

推荐阅读