mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-01-19 00:37:02 +00:00
Added test cases
This commit is contained in:
parent
525b945446
commit
f68690f296
|
@ -1,8 +1,34 @@
|
||||||
message = input("Encrypted message: ")
|
def decrypt(message):
|
||||||
|
"""
|
||||||
|
>>> decrypt('TMDETUX PMDVU')
|
||||||
|
Decryption using Key #0: TMDETUX PMDVU
|
||||||
|
Decryption using Key #1: SLCDSTW OLCUT
|
||||||
|
Decryption using Key #2: RKBCRSV NKBTS
|
||||||
|
Decryption using Key #3: QJABQRU MJASR
|
||||||
|
Decryption using Key #4: PIZAPQT LIZRQ
|
||||||
|
Decryption using Key #5: OHYZOPS KHYQP
|
||||||
|
Decryption using Key #6: NGXYNOR JGXPO
|
||||||
|
Decryption using Key #7: MFWXMNQ IFWON
|
||||||
|
Decryption using Key #8: LEVWLMP HEVNM
|
||||||
|
Decryption using Key #9: KDUVKLO GDUML
|
||||||
|
Decryption using Key #10: JCTUJKN FCTLK
|
||||||
|
Decryption using Key #11: IBSTIJM EBSKJ
|
||||||
|
Decryption using Key #12: HARSHIL DARJI
|
||||||
|
Decryption using Key #13: GZQRGHK CZQIH
|
||||||
|
Decryption using Key #14: FYPQFGJ BYPHG
|
||||||
|
Decryption using Key #15: EXOPEFI AXOGF
|
||||||
|
Decryption using Key #16: DWNODEH ZWNFE
|
||||||
|
Decryption using Key #17: CVMNCDG YVMED
|
||||||
|
Decryption using Key #18: BULMBCF XULDC
|
||||||
|
Decryption using Key #19: ATKLABE WTKCB
|
||||||
|
Decryption using Key #20: ZSJKZAD VSJBA
|
||||||
|
Decryption using Key #21: YRIJYZC URIAZ
|
||||||
|
Decryption using Key #22: XQHIXYB TQHZY
|
||||||
|
Decryption using Key #23: WPGHWXA SPGYX
|
||||||
|
Decryption using Key #24: VOFGVWZ ROFXW
|
||||||
|
Decryption using Key #25: UNEFUVY QNEWV
|
||||||
|
"""
|
||||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
||||||
|
|
||||||
message = message.upper()
|
|
||||||
|
|
||||||
for key in range(len(LETTERS)):
|
for key in range(len(LETTERS)):
|
||||||
translated = ""
|
translated = ""
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
|
@ -14,5 +40,14 @@ for key in range(len(LETTERS)):
|
||||||
translated = translated + LETTERS[num]
|
translated = translated + LETTERS[num]
|
||||||
else:
|
else:
|
||||||
translated = translated + symbol
|
translated = translated + symbol
|
||||||
|
|
||||||
print("Decryption using Key #%s: %s" % (key, translated))
|
print("Decryption using Key #%s: %s" % (key, translated))
|
||||||
|
|
||||||
|
def main():
|
||||||
|
message = input("Encrypted message: ")
|
||||||
|
message = message.upper()
|
||||||
|
decrypt(message)
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
main()
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# The Caesar Cipher Algorithm
|
# The Caesar Cipher Algorithm
|
||||||
|
|
||||||
|
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]: ")
|
||||||
|
@ -9,12 +10,23 @@ if mode.lower().startswith('e'):
|
||||||
elif mode.lower().startswith('d'):
|
elif mode.lower().startswith('d'):
|
||||||
mode = "decrypt"
|
mode = "decrypt"
|
||||||
|
|
||||||
LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
translated = encdec(message, key, mode)
|
||||||
|
if mode == "encrypt":
|
||||||
|
print("Encryption:", translated)
|
||||||
|
elif mode == "decrypt":
|
||||||
|
print("Decryption:", translated)
|
||||||
|
|
||||||
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"
|
||||||
for symbol in message:
|
for symbol in message:
|
||||||
if symbol in LETTERS:
|
if symbol in LETTERS:
|
||||||
num = LETTERS.find(symbol)
|
num = LETTERS.find(symbol)
|
||||||
|
@ -31,8 +43,9 @@ for symbol in message:
|
||||||
translated = translated + LETTERS[num]
|
translated = translated + LETTERS[num]
|
||||||
else:
|
else:
|
||||||
translated = translated + symbol
|
translated = translated + symbol
|
||||||
|
return translated
|
||||||
|
|
||||||
if mode == "encrypt":
|
if __name__ == '__main__':
|
||||||
print("Encryption:", translated)
|
import doctest
|
||||||
elif mode == "decrypt":
|
doctest.testmod()
|
||||||
print("Decryption:", translated)
|
main()
|
|
@ -28,9 +28,17 @@ def checkValidKey(key):
|
||||||
sys.exit('Error in the key or symbol set.')
|
sys.exit('Error in the key or symbol set.')
|
||||||
|
|
||||||
def encryptMessage(key, message):
|
def encryptMessage(key, message):
|
||||||
|
"""
|
||||||
|
>>> encryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Harshil Darji')
|
||||||
|
'Ilcrism Olcvs'
|
||||||
|
"""
|
||||||
return translateMessage(key, message, 'encrypt')
|
return translateMessage(key, message, 'encrypt')
|
||||||
|
|
||||||
def decryptMessage(key, message):
|
def decryptMessage(key, message):
|
||||||
|
"""
|
||||||
|
>>> decryptMessage('LFWOAYUISVKMNXPBDCRJTQEGHZ', 'Ilcrism Olcvs')
|
||||||
|
'Harshil Darji'
|
||||||
|
"""
|
||||||
return translateMessage(key, message, 'decrypt')
|
return translateMessage(key, message, 'decrypt')
|
||||||
|
|
||||||
def translateMessage(key, message, mode):
|
def translateMessage(key, message, mode):
|
||||||
|
|
|
@ -14,6 +14,10 @@ def main():
|
||||||
print('Output:\n%s' %(text + '|'))
|
print('Output:\n%s' %(text + '|'))
|
||||||
|
|
||||||
def encryptMessage(key, message):
|
def encryptMessage(key, message):
|
||||||
|
"""
|
||||||
|
>>> encryptMessage(6, 'Harshil Darji')
|
||||||
|
'Hlia rDsahrij'
|
||||||
|
"""
|
||||||
cipherText = [''] * key
|
cipherText = [''] * key
|
||||||
for col in range(key):
|
for col in range(key):
|
||||||
pointer = col
|
pointer = col
|
||||||
|
@ -23,6 +27,10 @@ def encryptMessage(key, message):
|
||||||
return ''.join(cipherText)
|
return ''.join(cipherText)
|
||||||
|
|
||||||
def decryptMessage(key, message):
|
def decryptMessage(key, message):
|
||||||
|
"""
|
||||||
|
>>> decryptMessage(6, 'Hlia rDsahrij')
|
||||||
|
'Harshil Darji'
|
||||||
|
"""
|
||||||
numCols = math.ceil(len(message) / key)
|
numCols = math.ceil(len(message) / key)
|
||||||
numRows = key
|
numRows = key
|
||||||
numShadedBoxes = (numCols * numRows) - len(message)
|
numShadedBoxes = (numCols * numRows) - len(message)
|
||||||
|
@ -40,4 +48,6 @@ def decryptMessage(key, message):
|
||||||
return "".join(plainText)
|
return "".join(plainText)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
main()
|
main()
|
||||||
|
|
|
@ -1,8 +1,11 @@
|
||||||
|
import os
|
||||||
|
|
||||||
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
UPPERLETTERS = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
|
||||||
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
|
LETTERS_AND_SPACE = UPPERLETTERS + UPPERLETTERS.lower() + ' \t\n'
|
||||||
|
|
||||||
def loadDictionary():
|
def loadDictionary():
|
||||||
dictionaryFile = open('Dictionary.txt')
|
path = os.path.split(os.path.realpath(__file__))
|
||||||
|
dictionaryFile = open(path[0] + '\Dictionary.txt')
|
||||||
englishWords = {}
|
englishWords = {}
|
||||||
for word in dictionaryFile.read().split('\n'):
|
for word in dictionaryFile.read().split('\n'):
|
||||||
englishWords[word] = None
|
englishWords[word] = None
|
||||||
|
@ -34,8 +37,19 @@ def removeNonLetters(message):
|
||||||
return ''.join(lettersOnly)
|
return ''.join(lettersOnly)
|
||||||
|
|
||||||
def isEnglish(message, wordPercentage = 20, letterPercentage = 85):
|
def isEnglish(message, wordPercentage = 20, letterPercentage = 85):
|
||||||
|
"""
|
||||||
|
>>> isEnglish('Hello World')
|
||||||
|
True
|
||||||
|
|
||||||
|
>>> isEnglish('llold HorWd')
|
||||||
|
False
|
||||||
|
"""
|
||||||
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
|
wordsMatch = getEnglishCount(message) * 100 >= wordPercentage
|
||||||
numLetters = len(removeNonLetters(message))
|
numLetters = len(removeNonLetters(message))
|
||||||
messageLettersPercentage = (float(numLetters) / len(message)) * 100
|
messageLettersPercentage = (float(numLetters) / len(message)) * 100
|
||||||
lettersMatch = messageLettersPercentage >= letterPercentage
|
lettersMatch = messageLettersPercentage >= letterPercentage
|
||||||
return wordsMatch and lettersMatch
|
return wordsMatch and lettersMatch
|
||||||
|
|
||||||
|
|
||||||
|
import doctest
|
||||||
|
doctest.testmod()
|
||||||
|
|
Loading…
Reference in New Issue
Block a user