python 版 Atbash 密码
假设我们有一个名为 text 的小写字母字符串。我们必须找到一个新字符串,其中 text 中的每个字母都映射到它在字母表中的反序。例如,a 变成 z,b 变成 y,依此类推。
因此,如果输入是“abcdefg”,则输出将是“zyxwvut”
为了解决这个问题,我们将按照以下步骤进行 −
N := ('z') 的 ASCII 码 + ('a') 的 ASCII 码
连接 text 中每个字符 ASCII 值 (N - s 的 ASCII 码) 构成的 ans 作为返回结果
让我们看看以下实现以加深理解 −
示例
class Solution: def solve(self, text): N = ord('z') + ord('a') ans='' return ans.join([chr(N - ord(s)) for s in text]) ob = Solution() print(ob.solve("abcdefg")) print(ob.solve("hello"))
输入
"abcdefg" "hello"
Learn Python in-depth with real-world projects through our Python certification course. Enroll and become a certified expert to boost your career.
输出
zyxwvut svool
广告