mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-31 06:33:44 +00:00
Merge pull request #46 from Henocks/patch-2
Slight Performance/Visual Update
This commit is contained in:
commit
3f505c5b39
|
@ -2,8 +2,8 @@
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
message = input("Enter message: ")
|
message = input("Enter message: ")
|
||||||
key = int(input("Key [1-26]: "))
|
key = int(input("Key [1-26]: "))
|
||||||
mode = input("Encrypt or Decrypt [e/d]: ")
|
mode = input("Encrypt or Decrypt [e/d]: ")
|
||||||
|
|
||||||
if mode.lower().startswith('e'):
|
if mode.lower().startswith('e'):
|
||||||
mode = "encrypt"
|
mode = "encrypt"
|
||||||
|
@ -11,41 +11,34 @@ def main():
|
||||||
mode = "decrypt"
|
mode = "decrypt"
|
||||||
|
|
||||||
translated = encdec(message, key, mode)
|
translated = encdec(message, key, mode)
|
||||||
if mode == "encrypt":
|
if mode == "encrypt":
|
||||||
print("Encryption:", translated)
|
print("Encryption:", translated)
|
||||||
elif mode == "decrypt":
|
elif mode == "decrypt":
|
||||||
print("Decryption:", translated)
|
print("Decryption:", translated)
|
||||||
|
|
||||||
def encdec(message, key, mode):
|
def encdec(message, key, mode):
|
||||||
"""
|
message = message.upper()
|
||||||
>>> encdec('Harshil Darji', 12, 'encrypt')
|
|
||||||
'TMDETUX PMDVU'
|
|
||||||
|
|
||||||
>>> encdec('TMDETUX PMDVU', 12, 'decrypt')
|
|
||||||
'HARSHIL DARJI'
|
|
||||||
"""
|
|
||||||
message = message.upper()
|
|
||||||
translated = ""
|
translated = ""
|
||||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in LETTERS:
|
if symbol in LETTERS:
|
||||||
num = LETTERS.find(symbol)
|
num = LETTERS.find(symbol)
|
||||||
if mode == "encrypt":
|
if mode == "encrypt":
|
||||||
num = num + key
|
num = num + key
|
||||||
elif mode == "decrypt":
|
elif mode == "decrypt":
|
||||||
num = num - key
|
num = num - key
|
||||||
|
|
||||||
if num >= len(LETTERS):
|
if num >= len(LETTERS):
|
||||||
num = num - len(LETTERS)
|
num -= len(LETTERS)
|
||||||
elif num < 0:
|
elif num < 0:
|
||||||
num = num + len(LETTERS)
|
num += len(LETTERS)
|
||||||
|
|
||||||
translated = translated + LETTERS[num]
|
translated += LETTERS[num]
|
||||||
else:
|
else:
|
||||||
translated = translated + symbol
|
translated += symbol
|
||||||
return translated
|
return translated
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
import doctest
|
import doctest
|
||||||
doctest.testmod()
|
doctest.testmod()
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user