Last active
January 26, 2017 21:08
-
-
Save JeffCohen/980821f3508062f89700ce88abd6b3a5 to your computer and use it in GitHub Desktop.
This file contains 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
# (Run this code with Python 3 only.) | |
# | |
# This program decodes a secret message by advancing each | |
# letter by 2 places. Spaces and punctuation should remain | |
# unchanged. | |
# Examples: | |
# "nwrfml" decodes into: "python" | |
# "ynnjc" decodes into: "apple" | |
# | |
# Notice how the encoded 'y' wraps back to 'a'. | |
# | |
# How would you rewrite this code to be more readable? | |
# ---------------------------------------------------- | |
def decode(message): | |
plain_message = "" | |
for character in message: | |
if ord(character) >= 97 and ord(character) <= (122): | |
decoded_value = ord(character) + 2 | |
decoded_value = decoded_value if decoded_value <= 122 else (96 + decoded_value - 122) | |
plain_message += chr(decoded_value) | |
else: | |
plain_message += character | |
return plain_message | |
secret_message = "g fmnc wms bgbl'r rpylqjyrc gr zw fylb. \ | |
rfyr'q ufyr amknsrcpq ypc dmp." | |
decoded_message = decode(secret_message) | |
print(decoded_message) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment