Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Global variable
- alphabet = "abcdefghijklmnopqrstuvwxyz"
- # **********************************************************************************************************************
- # **********************************************************************************************************************
- # MY FUNCTION
- def code(message, num):
- # 1. I create the map/directory: 'a' --> 1, 'b' ---> 2, ....
- counter = 0
- directory = {}
- for ch in alphabet:
- counter += 1
- directory[ch] = counter
- # print(directory)
- # 2. Using this map/directory, I will convert the message into a sequence of numbers
- sequence = [] # List og integer numbers
- for ch in message:
- # For every character "ch" in the string "message": I will scan all the directory to find matching characters
- for key, value in directory.items():
- if ch.lower() == key:
- sequence.append(value)
- # print(sequence)
- # 3. I will alter the elements of the sequence based on the following rule:
- # If sequence[i] = x, then the new value will be: (x * num) % 26
- # Like a function: f(x): x ---> (x * num) % 26
- for i in range(0, len(sequence)):
- sequence[i] = (sequence[i] * num) % len(alphabet)
- # print(sequence)
- # 4. Then, having the numbers in the list "sequence", I match them with the appropriate characters of the alphabet
- codedMessage = ""
- for i in range(0, len(sequence)):
- # For every number in my list, I will scan all the directory: And when the number is equal to a value of a directory, I do the following
- for key, value in directory.items():
- if sequence[i] == value:
- codedMessage += key
- return codedMessage
- # **********************************************************************************************************************
- # **********************************************************************************************************************
- # MAIN FUNCTION
- print()
- print("Give me a message and then a number to cipher the given message. Be careful! It must contain only lowercase letters!")
- print("If you contain any numbers or special characters in the string/message, they will be ignored...")
- message = input("Message: ")
- number = int(input("Number: "))
- codedMessage = code(message, number)
- print("Coded Message: " + codedMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement