Created
September 25, 2014 21:05
-
-
Save davidallsopp/8032fa2896de1f7f08fb to your computer and use it in GitHub Desktop.
Code club: My take on the crypto example from Python for Kids: http://jasonrbriggs.com/python-for-kids/puzzles/puzzle2.html and http://jasonrbriggs.com/python-for-kids/puzzles/puzzle2-solution.html
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
| letters = 'abcdefghijklmnopqrstuvwxyz ' | |
| jumbled = 'jklmnopqrstuvwxyzabcdefghi ' | |
| # Long way | |
| #decrypt_map = { } | |
| #encrypt_map = { } | |
| #for x in range(len(encrypted_letters)): | |
| # encrypted_letter = encrypted_letters[x] | |
| # unencrypted_letter = unencrypted_letters[x] | |
| # decrypt_map[unencrypted_letter] = encrypted_letter | |
| # encrypt_map[encrypted_letter] = unencrypted_letter | |
| # Short way | |
| decrypt_map = dict(zip(letters,jumbled)) | |
| encrypt_map = dict(zip(jumbled,letters)) | |
| # Short way | |
| def decrypt(text): | |
| return ''.join([decrypt_map[a] for a in text]) | |
| # Long way | |
| def encrypt(text): | |
| rtn = [ ] | |
| for l in text: | |
| rtn.append(encrypt_map[l]) | |
| return ''.join(rtn) | |
| print(decrypt('nyve r tcftb zj jkzcc ylexip zk xfvj srtb wfli jvtfeuj')) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment