Atbash cipher in Python


Suppose we have a lowercase alphabet string called text. We have to find a new string where every character in text is mapped to its reverse in the alphabet. As an example, a becomes z, b becomes y and so on.

So, if the input is like "abcdefg", then the output will be "zyxwvut"

To solve this, we will follow these steps −

  • N := ASCII of ('z') + ASCII of ('a')

  • return ans by joining each character from ASCII value (N - ASCII of s) for each character s in text

Let us see the following implementation to get better understanding −

Example

 Live Demo

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"))

Input

"abcdefg"
"hello"

Output

zyxwvut
svool

Updated on: 02-Sep-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements