Created
July 4, 2015 20:12
-
-
Save svanellewee/e1cff505f01f503e6b26 to your computer and use it in GitHub Desktop.
My solution for http://puzzles.bostonpython.com/poetry.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
| data = '''a narrow fellow in the grass | |
| occasionally rides; | |
| you may have met him, did you not, | |
| his notice sudden is. | |
| the grass divides as with a comb, | |
| a spotted shaft is seen; | |
| and then it closes at your feet | |
| and opens further on. | |
| he likes a boggy acre, | |
| a floor too cool for corn. | |
| yet when a child, and barefoot, | |
| i more than once, at morn, | |
| have passed, i thought, a whip-lash | |
| unbraiding in the sun, | |
| when, stooping to secure it, | |
| it wrinkled, and was gone. | |
| several of nature's people | |
| i know, and they know me; | |
| i feel for them a transport | |
| of cordiality; | |
| but never met this fellow, | |
| attended or alone, | |
| without a tighter breathing, | |
| and zero at the bone.''' | |
| import re | |
| def getchars(data): | |
| return re.findall("([a-zA-Z]{1})", data) | |
| return | |
| def print_mapping(): | |
| chars = getchars(data) | |
| mapping = dict(zip(map(chars.count,chars), chars)) | |
| print "\n".join("%s=>> %s"%(x,y) for x,y in sorted(mapping.items())) | |
| print_mapping() | |
| def encode(word): | |
| chars = getchars(data) | |
| reverselookup = dict(zip(chars, map(chars.count,chars))) | |
| return map(reverselookup.get, word) | |
| def decode(cypher): | |
| chars = getchars(data) | |
| lookup = dict(zip(map(chars.count,chars), chars)) | |
| return "".join(map(lookup.get, cypher)) | |
| assert(encode("enter") == [ 56,38, 44, 56,29]) | |
| assert(encode("zebra") == [1,56,7,29,42] ) | |
| cypher = [56,38,44,56,29] | |
| print "Answer is '%s'"%decode(cypher) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment