MCPcopy Create free account
hub / github.com/TheAlgorithms/Python / translate_message

Function translate_message

ciphers/vigenere_cipher.py:36–61  ·  view source on GitHub ↗
(key: str, message: str, mode: str)

Source from the content-addressed store, hash-verified

34
35
36def translate_message(key: str, message: str, mode: str) -> str:
37 translated = []
38 key_index = 0
39 key = key.upper()
40
41 for symbol in message:
42 num = LETTERS.find(symbol.upper())
43 if num != -1:
44 if mode == "encrypt":
45 num += LETTERS.find(key[key_index])
46 elif mode == "decrypt":
47 num -= LETTERS.find(key[key_index])
48
49 num %= len(LETTERS)
50
51 if symbol.isupper():
52 translated.append(LETTERS[num])
53 elif symbol.islower():
54 translated.append(LETTERS[num].lower())
55
56 key_index += 1
57 if key_index == len(key):
58 key_index = 0
59 else:
60 translated.append(symbol)
61 return "".join(translated)
62
63
64if __name__ == "__main__":

Callers 2

encrypt_messageFunction · 0.70
decrypt_messageFunction · 0.70

Calls 2

findMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected