-
-
Save MarieAshley/21c39127de0a57cc6780 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random, unittest | |
def generateMagicKey(): | |
magicKey = {} | |
list1 = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] | |
list2 = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"] | |
while len(list2) > 0: | |
z = random.randint(0, len(list2)-1) | |
y = random.randint(0, len(list2)-1) | |
a, b = list1[z], list2[y] | |
magicKey[a], magicKey[b] = b, a | |
list1.remove(a) | |
list2.remove(a) | |
try: list2.remove(b) | |
except: pass | |
try: list1.remove(b) | |
except: pass | |
print "Your magic key is:\n" | |
print magicKey | |
return magicKey | |
def encrypt(word, magicKey): | |
stringBuild = "" | |
for i in word: | |
i = i.lower() | |
if i in magicKey.keys(): stringBuild += magicKey[i.lower()] | |
else: stringBuild += i | |
return stringBuild | |
def decrypt(word, magicKey): | |
stringBuild = "" | |
for i in word: | |
if i in magicKey.keys(): stringBuild += magicKey[i] | |
else: stringBuild += i | |
return stringBuild | |
class Test_phrases(unittest.TestCase): | |
s = "Sunshine, daisies, butter mellow, turn this stupid, fat rat yellow." | |
print(s) | |
magicKey = generateMagicKey() | |
newWord = encrypt(s, magicKey) | |
print(newWord) | |
def test_poem(self): | |
decrypted = decrypt(self.newWord, self.magicKey) | |
print(decrypted) | |
self.assertEqual(decrypted, self.s.lower()) | |
if __name__ == "__main__": | |
unittest.main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Added ability to encrypt and decrypt phrases.