mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-23 21:11:08 +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>
66 lines
1.7 KiB
Python
66 lines
1.7 KiB
Python
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
|
|
|
|
|
def main() -> None:
|
|
message = input("Enter message: ")
|
|
key = input("Enter key [alphanumeric]: ")
|
|
mode = input("Encrypt/Decrypt [e/d]: ")
|
|
|
|
if mode.lower().startswith("e"):
|
|
mode = "encrypt"
|
|
translated = encrypt_message(key, message)
|
|
elif mode.lower().startswith("d"):
|
|
mode = "decrypt"
|
|
translated = decrypt_message(key, message)
|
|
|
|
print(f"\n{mode.title()}ed message:")
|
|
print(translated)
|
|
|
|
|
|
def encrypt_message(key: str, message: str) -> str:
|
|
"""
|
|
>>> encrypt_message('HDarji', 'This is Harshil Darji from Dharmaj.')
|
|
'Akij ra Odrjqqs Gaisq muod Mphumrs.'
|
|
"""
|
|
return translate_message(key, message, "encrypt")
|
|
|
|
|
|
def decrypt_message(key: str, message: str) -> str:
|
|
"""
|
|
>>> decrypt_message('HDarji', 'Akij ra Odrjqqs Gaisq muod Mphumrs.')
|
|
'This is Harshil Darji from Dharmaj.'
|
|
"""
|
|
return translate_message(key, message, "decrypt")
|
|
|
|
|
|
def translate_message(key: str, message: str, mode: str) -> str:
|
|
translated = []
|
|
key_index = 0
|
|
key = key.upper()
|
|
|
|
for symbol in message:
|
|
num = LETTERS.find(symbol.upper())
|
|
if num != -1:
|
|
if mode == "encrypt":
|
|
num += LETTERS.find(key[key_index])
|
|
elif mode == "decrypt":
|
|
num -= LETTERS.find(key[key_index])
|
|
|
|
num %= len(LETTERS)
|
|
|
|
if symbol.isupper():
|
|
translated.append(LETTERS[num])
|
|
elif symbol.islower():
|
|
translated.append(LETTERS[num].lower())
|
|
|
|
key_index += 1
|
|
if key_index == len(key):
|
|
key_index = 0
|
|
else:
|
|
translated.append(symbol)
|
|
return "".join(translated)
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|