mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-12-18 01:00:15 +00:00
07e991d553
* ci(pre-commit): Add pep8-naming to `pre-commit` hooks (#7038) * refactor: Fix naming conventions (#7038) * Update arithmetic_analysis/lu_decomposition.py Co-authored-by: Christian Clauss <cclauss@me.com> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * refactor(lu_decomposition): Replace `NDArray` with `ArrayLike` (#7038) * chore: Fix naming conventions in doctests (#7038) * fix: Temporarily disable project euler problem 104 (#7069) * chore: Fix naming conventions in doctests (#7038) Co-authored-by: Christian Clauss <cclauss@me.com> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
79 lines
1.8 KiB
Python
79 lines
1.8 KiB
Python
import random
|
|
import sys
|
|
|
|
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
def main() -> None:
|
|
message = input("Enter message: ")
|
|
key = "LFWOAYUISVKMNXPBDCRJTQEGHZ"
|
|
resp = input("Encrypt/Decrypt [e/d]: ")
|
|
|
|
check_valid_key(key)
|
|
|
|
if resp.lower().startswith("e"):
|
|
mode = "encrypt"
|
|
translated = encrypt_message(key, message)
|
|
elif resp.lower().startswith("d"):
|
|
mode = "decrypt"
|
|
translated = decrypt_message(key, message)
|
|
|
|
print(f"\n{mode.title()}ion: \n{translated}")
|
|
|
|
|
|
def check_valid_key(key: str) -> None:
|
|
key_list = list(key)
|
|
letters_list = list(LETTERS)
|
|
key_list.sort()
|
|
letters_list.sort()
|
|
|
|
if key_list != letters_list:
|
|
sys.exit("Error in the key or symbol set.")
|
|
|
|
|
|
def encrypt_message(key: str, message: str) -> str:
|
|
"""
|
|
>>> encrypt_message('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji')
|
|
'Ilcrism Olcvs'
|
|
"""
|
|
return translate_message(key, message, "encrypt")
|
|
|
|
|
|
def decrypt_message(key: str, message: str) -> str:
|
|
"""
|
|
>>> decrypt_message('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs')
|
|
'Harshil Darji'
|
|
"""
|
|
return translate_message(key, message, "decrypt")
|
|
|
|
|
|
def translate_message(key: str, message: str, mode: str) -> str:
|
|
translated = ""
|
|
chars_a = LETTERS
|
|
chars_b = key
|
|
|
|
if mode == "decrypt":
|
|
chars_a, chars_b = chars_b, chars_a
|
|
|
|
for symbol in message:
|
|
if symbol.upper() in chars_a:
|
|
sym_index = chars_a.find(symbol.upper())
|
|
if symbol.isupper():
|
|
translated += chars_b[sym_index].upper()
|
|
else:
|
|
translated += chars_b[sym_index].lower()
|
|
else:
|
|
translated += symbol
|
|
|
|
return translated
|
|
|
|
|
|
def get_random_key() -> str:
|
|
key = list(LETTERS)
|
|
random.shuffle(key)
|
|
return "".join(key)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|