Advertisement
makispaiktis

Coding a message with % 26 tactics (Codewars)

Oct 29th, 2019 (edited)
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.41 KB | None | 0 0
  1. # Global variable
  2. alphabet = "abcdefghijklmnopqrstuvwxyz"
  3.  
  4. # **********************************************************************************************************************
  5. # **********************************************************************************************************************
  6. # MY FUNCTION
  7. def code(message, num):
  8.  
  9.     # 1. I create the map/directory: 'a' --> 1, 'b' ---> 2, ....
  10.     counter = 0
  11.     directory = {}
  12.     for ch in alphabet:
  13.         counter += 1
  14.         directory[ch] = counter
  15.     # print(directory)
  16.  
  17.     # 2. Using this map/directory, I will convert the message into a sequence of numbers
  18.     sequence = []                            # List og integer numbers
  19.     for ch in message:
  20.         # For every character "ch" in the string "message": I will scan all the directory to find matching characters
  21.         for key, value in directory.items():
  22.             if ch.lower() == key:
  23.                 sequence.append(value)
  24.     # print(sequence)
  25.  
  26.     # 3. I will alter the elements of the sequence based on the following rule:
  27.     # If sequence[i] = x, then the new value will be: (x * num) % 26
  28.     # Like a function:      f(x): x ---> (x * num) % 26
  29.     for i in range(0, len(sequence)):
  30.         sequence[i] = (sequence[i] * num) % len(alphabet)
  31.  
  32.     # print(sequence)
  33.  
  34.     # 4. Then, having the numbers in the list "sequence", I match them with the appropriate characters of the alphabet
  35.     codedMessage = ""
  36.     for i in range(0, len(sequence)):
  37.         # 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
  38.         for key, value in directory.items():
  39.             if sequence[i] == value:
  40.                 codedMessage += key
  41.  
  42.     return codedMessage
  43.  
  44.  
  45. # **********************************************************************************************************************
  46. # **********************************************************************************************************************
  47. # MAIN FUNCTION
  48. print()
  49. print("Give me a message and then a number to cipher the given message. Be careful! It must contain only lowercase letters!")
  50. print("If you contain any numbers or special characters in the string/message, they will be ignored...")
  51. message = input("Message: ")
  52. number = int(input("Number: "))
  53. codedMessage = code(message, number)
  54. print("Coded Message: " + codedMessage)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement