Instructions
Requirements and Specifications
Source Code
def main():
# Define the alphabet
alphabet = 'abcdefghijklmnopqrstuvwxyz'
# Now, ask user for letter code and number
while True:
code = input("Enter letter code: ")
code = code.lower()
if code in alphabet:
break
else:
print("Please enter a letter from the alphabet.")
# Ask for number
while True:
N = input("Enter number (shift): ")
if N.isnumeric():
N = int(N)
break
else:
print("Please enter a valid number.")
# Now, construct the key by right-shifting the alphabet N positions starting at code
code_index = alphabet.index(code) # Index of the letter code
encoded_index = code_index + N # Index of the letter to substitute the letter code
# If the new index is higher than the length of the alphabet, then subtract the len of the alphabet from that index
# This will make the alphabet to be arranged in a circle
if(encoded_index > len(alphabet)-1):
encoded_index = len(alphabet) - 1 - encoded_index
# Encoded letter
encoded_letter = alphabet[encoded_index]
# Construct dict with key
# For a letter code 'A' and a shift of 3, the dictionary will contain the following items
# {'a': 'd', 'b': 'e', 'c', 'f', 'd': 'g', ...}
key = dict()
i = 0
# Now, loop through the alphabet and assign the encoded letter in the dictionary
for letter in alphabet:
key[letter] = alphabet[encoded_index]
encoded_index += 1
if(encoded_index > len(alphabet)-1):
encoded_index = 0
"""
UNCOMMENT THE FOLLOWING LINE IF YOU WANT TO SEE THE DICTIONARY WITH THE KEY OF THE CIPHER
"""
# print(key)
# Now, ask user for string to encode
text = input("Entet text to encode: ")
# Encode
encoded_text = encode(text, key)
# Decode
decoded_text = decode(encoded_text, key)
print("")
print("The encoded text is: " + encoded_text)
print("")
print("The decoded text is: " + decoded_text)
def encode(text, key):
"""
Given a text and a dictionary containing a key, this function returns an encoded version of text
:param text: Text to encode
:param key: Dictionary containing the key
return: Encoded text
"""
encoded_text = ""
for c in text: # Loop through each char in the text
c_l = c.lower() # Convert it to lower case
if c_l in key: # Check if the character is in the alphabet
c_coded = key[c_l] # code
if c.isupper(): # check if the original character is upper case
c_coded = c_coded.upper()
encoded_text += c_coded
else: # The character is not in the alphabet. Probably a sign of exclamation, a comma, etc
encoded_text += c
return encoded_text
def decode(text, key):
"""
Given a text and a dictionary containing a key, this function returns a decoded version of text
:param text: Encoded text
:param key: Dictionary containing the key
:return: Decoded version of text
"""
# We have to reverse the dictionary containing the key
key = {value: k for (k, value) in key.items()}
decoded_text = ""
for c in text: # Loop through each char in the text
c_l = c.lower() # Convert it to lower case
if c_l in key: # Check if the character is in the alphabet
c_coded = key[c_l] # decode
if c.isupper(): # check if the original character is upper case
c_coded = c_coded.upper()
decoded_text += c_coded
else: # The character is not in the alphabet. Probably a sign of exclamation, a comma, etc
decoded_text += c
return decoded_text
if __name__ == '__main__':
main()