|
2 | 2 |
|
3 | 3 | import string |
4 | 4 |
|
| 5 | + |
5 | 6 | def atbash(text: str) -> str: |
6 | 7 | """ |
7 | 8 | Encodes or decodes text using the Atbash cipher. |
8 | | - |
| 9 | +
|
9 | 10 | The Atbash cipher substitutes each letter with its mirror in the alphabet: |
10 | 11 | A -> Z, B -> Y, C -> X, ... Z -> A (case is preserved) |
11 | 12 | Non-alphabetic characters are left unchanged. |
12 | | - |
| 13 | +
|
13 | 14 | Args: |
14 | 15 | text: The input string to encode/decode |
15 | | - |
| 16 | +
|
16 | 17 | Returns: |
17 | 18 | The transformed string |
18 | 19 | """ |
19 | 20 | # Create translation tables for uppercase and lowercase |
20 | | - lowercase_map = str.maketrans( |
21 | | - string.ascii_lowercase, |
22 | | - string.ascii_lowercase[::-1] |
23 | | - ) |
24 | | - uppercase_map = str.maketrans( |
25 | | - string.ascii_uppercase, |
26 | | - string.ascii_uppercase[::-1] |
27 | | - ) |
28 | | - |
| 21 | + lowercase_map = str.maketrans(string.ascii_lowercase, string.ascii_lowercase[::-1]) |
| 22 | + uppercase_map = str.maketrans(string.ascii_uppercase, string.ascii_uppercase[::-1]) |
| 23 | + |
29 | 24 | # Apply both translation mappings |
30 | 25 | return text.translate(lowercase_map).translate(uppercase_map) |
31 | 26 |
|
| 27 | + |
32 | 28 | # Example usage |
33 | 29 | if __name__ == "__main__": |
34 | 30 | test_string = "Hello, World! 123" |
35 | 31 | encoded = atbash(test_string) |
36 | 32 | decoded = atbash(encoded) |
37 | | - |
| 33 | + |
38 | 34 | print(f"Original: {test_string}") |
39 | 35 | print(f"Encoded: {encoded}") |
40 | 36 | print(f"Decoded: {decoded}") |
0 commit comments