>>> translate_message("QWERTYUIOPASDFGHJKLZXCVBNM","Hello World","encrypt") 'Pcssi Bidsm'
(
key: str, message: str, mode: Literal["encrypt", "decrypt"]
)
| 4 | |
| 5 | |
| 6 | def translate_message( |
| 7 | key: str, message: str, mode: Literal["encrypt", "decrypt"] |
| 8 | ) -> str: |
| 9 | """ |
| 10 | >>> translate_message("QWERTYUIOPASDFGHJKLZXCVBNM","Hello World","encrypt") |
| 11 | 'Pcssi Bidsm' |
| 12 | """ |
| 13 | chars_a = LETTERS if mode == "decrypt" else key |
| 14 | chars_b = key if mode == "decrypt" else LETTERS |
| 15 | translated = "" |
| 16 | # loop through each symbol in the message |
| 17 | for symbol in message: |
| 18 | if symbol.upper() in chars_a: |
| 19 | # encrypt/decrypt the symbol |
| 20 | sym_index = chars_a.find(symbol.upper()) |
| 21 | if symbol.isupper(): |
| 22 | translated += chars_b[sym_index].upper() |
| 23 | else: |
| 24 | translated += chars_b[sym_index].lower() |
| 25 | else: |
| 26 | # symbol is not in LETTERS, just add it |
| 27 | translated += symbol |
| 28 | return translated |
| 29 | |
| 30 | |
| 31 | def encrypt_message(key: str, message: str) -> str: |
no test coverage detected