Created
February 1, 2018 17:48
-
-
Save ahmed-bhs/5497f3fe14003a7c54a621b7fb0e1902 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
# ROT Encoder/Decoder now with input validation. | |
import sys | |
# Sets alphabet characters for comparison. | |
alphabet = ["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"] | |
# Performs ROTation based on user input. | |
def rotCalc(userStr): | |
output = "" | |
for i in range(len(userStr)): | |
# Handles whitespace. | |
if userStr[i] == " ": | |
output = output + " " | |
# Handles digits - in default ROT encoding these do not rotate. | |
elif userStr[i].isdigit(): | |
output = output + userStr[i] | |
# Handles alphabetical characters. | |
elif userStr[i].isalpha(): | |
for j in range(len(alphabet)): | |
# Handles uppercase characters. | |
if userStr[i].isupper(): | |
if userStr[i].lower() == alphabet[j]: | |
if encOrDec == 1: | |
if j + rotNum > 25: | |
diff = (j + rotNum) - 25 | |
output = output + (alphabet[diff - 1].upper()) | |
else: | |
output = output + (alphabet[j + rotNum].upper()) | |
elif encOrDec == 2: | |
if j - rotNum < 0: | |
diff = (j - rotNum) + 25 | |
output = output + (alphabet[diff + 1].upper()) | |
else: | |
output = output + (alphabet[j - rotNum].upper()) | |
# Handles lowercase characters. | |
elif userStr[i] == alphabet[j]: | |
if encOrDec == 1: | |
if j + rotNum > 25: | |
diff = (j + rotNum) - 25 | |
output = output + (alphabet[diff - 1]) | |
else: | |
output = output + (alphabet[j + rotNum]) | |
elif encOrDec == 2: | |
if j - rotNum < 0: | |
diff = (j - rotNum) + 25 | |
output = output + (alphabet[diff + 1]) | |
else: | |
output = output + (alphabet[j - rotNum]) | |
# Handles anything else (special characters like !@#$, etc). | |
else: | |
output = output + userStr[i] | |
return output | |
# Main program loop requests encode/decode, ROTation number, and user string. Then sends to rotCalc function. | |
# The main program implements input validation ensuring legal input from the user; quits on illegal input. | |
while True: | |
encOrDec = raw_input("Enter 1 to ENCODE, 2 to DECODE, anything else to quit: ") | |
userStr = raw_input("Enter your string: ") | |
if encOrDec.isdigit() == False: | |
print "Goodbye!" | |
sys.exit() | |
else: | |
encOrDec = int(encOrDec) | |
if encOrDec == 1 or encOrDec == 2: | |
for rotNum in range(1,25): | |
rotNum = int(rotNum) | |
if rotNum >= 1 and rotNum < 26: | |
# Prints final encoded/decoded string | |
if encOrDec == 1: | |
print userStr, ("ROT-" + str(rotNum)), "encoded:", rotCalc(userStr) | |
elif encOrDec == 2: | |
print userStr, ("ROT-" + str(rotNum)), "decoded:", rotCalc(userStr) | |
else: | |
print "Goodbye!" | |
sys.exit() | |
else: | |
print "Goodbye!" | |
sys.exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment