Skip to content

Commit

Permalink
Merge pull request TheAlgorithms#46 from Henocks/patch-2
Browse files Browse the repository at this point in the history
Slight Performance/Visual Update
  • Loading branch information
harshildarji authored Nov 9, 2016
2 parents 18afe45 + 4351380 commit 3f505c5
Showing 1 changed file with 11 additions and 18 deletions.
29 changes: 11 additions & 18 deletions ciphers/caesar_cipher.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,43 @@

def main():
message = input("Enter message: ")
key = int(input("Key [1-26]: "))
mode = input("Encrypt or Decrypt [e/d]: ")
key = int(input("Key [1-26]: "))
mode = input("Encrypt or Decrypt [e/d]: ")

if mode.lower().startswith('e'):
mode = "encrypt"
elif mode.lower().startswith('d'):
mode = "decrypt"

translated = encdec(message, key, mode)
if mode == "encrypt":
if mode == "encrypt":
print("Encryption:", translated)
elif mode == "decrypt":
print("Decryption:", translated)

def encdec(message, key, mode):
"""
>>> encdec('Harshil Darji', 12, 'encrypt')
'TMDETUX PMDVU'
>>> encdec('TMDETUX PMDVU', 12, 'decrypt')
'HARSHIL DARJI'
"""
message = message.upper()
message = message.upper()
translated = ""
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for symbol in message:
if symbol in LETTERS:
num = LETTERS.find(symbol)
if mode == "encrypt":
if mode == "encrypt":
num = num + key
elif mode == "decrypt":
num = num - key

if num >= len(LETTERS):
num = num - len(LETTERS)
num -= len(LETTERS)
elif num < 0:
num = num + len(LETTERS)
num += len(LETTERS)

translated = translated + LETTERS[num]
translated += LETTERS[num]
else:
translated = translated + symbol
translated += symbol
return translated

if __name__ == '__main__':
import doctest
doctest.testmod()
main()
main()

0 comments on commit 3f505c5

Please sign in to comment.