Advertisement
makispaiktis

CodeJam - Problem3: Encryption with primes

Apr 23rd, 2019 (edited)
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 4.66 KB | None | 0 0
  1. import math
  2.  
  3. # 1. Create a list with all the primes
  4. primes = [2]                                        # Firstly, my list primes contains only the number 2
  5. counter = 0
  6.  
  7. for i in range(3, 1000):
  8.     flag = True                                     # Initially, I suppose that flag = true ---> my number is prime
  9.     for j in range(2, math.floor(i/2)):
  10.         if i % j == 0:
  11.             flag = False                            # If I find a "j": i % j == 0 ---> i can be divided with j ---> i NOT PRIME
  12.             break
  13.  
  14.     if flag == True:                                # That means that j-loop didn't go in if-statement for NOT PRIMES
  15.         primes.append(i)
  16.  
  17. # GIA KAPOIO LOGO MOU VAZEI KAI TO '4' SAN PRIME OPOTE TON KANW REMOVE
  18. primes.remove(4)
  19.  
  20. # Display the primes from 1 to 1000
  21. #print(primes)
  22. #print()
  23.  
  24. # 2. Create a list named alphabet containing all the letters in the correct row
  25. word = "abcdefghijklmnopqrstuvwxyz"
  26. alphabet = []
  27. for ch in word:
  28.     alphabet.append(ch)
  29.  
  30. #print(alphabet)
  31. #print()
  32.  
  33. # 3. Ask the user to give me the appropriate numbers
  34. print("*******************************************************************************************")
  35. print("*******************************************************************************************")
  36. length = int(input("Enter how many numbers you will give me (this length + 1 will be the length of the word:): "))
  37. numbers = []                                            # I will fill the list with numbers given by the user (list.size will be length)
  38. for i in range(length):
  39.     userNumber = int(input("Give me the number " + str(i+1) + ": "))
  40.     numbers.append(userNumber)
  41.  
  42. print()
  43. print("  Your numbers   : " + str(numbers))
  44.  
  45. # 4. Spaw to 1o ginomeno, gia na exw to 1o psifio kai meta kanw "length" epanalipseis
  46. diairetis1 = 1
  47. for i in range(len(primes)):
  48.     if numbers[0] % primes[i] == 0:
  49.         diairetis1 = primes[i]
  50.         break
  51. diairetis2 = numbers[0] / diairetis1
  52.  
  53. diairetis3 = 1
  54. for i in range(len(primes)):
  55.     if numbers[1] % primes[i] == 0:
  56.         diairetis3 = primes[i]
  57.         break
  58. diairetis4 = numbers[1] / diairetis3
  59.  
  60. # Arxizw na ftiaxnw tin lista me ta noumera-apantiseis
  61. wordNumbers = []
  62. arxi = 0
  63.  
  64. '''
  65. if diairetis1 == diairetis3:
  66.    arxi = diairetis2
  67. if diairetis1 == diairetis4:
  68.    arxi = diairetis2
  69. if diairetis2 == diairetis3:
  70.    arxi = diairetis1
  71. if diairetis2 == diairetis4:
  72.    arxi = diairetis1
  73.  
  74. wordNumbers.append(arxi)
  75. print(arxi)
  76. '''
  77.  
  78. for check in range(length-1):
  79.     diairetis1 = 1
  80.     for i in range(len(primes)):
  81.         if numbers[check] % primes[i] == 0:
  82.             diairetis1 = primes[i]
  83.             break
  84.     diairetis2 = numbers[check] / diairetis1
  85.  
  86.     diairetis3 = 1
  87.     for i in range(len(primes)):
  88.         if numbers[check+1] % primes[i] == 0:
  89.             diairetis3 = primes[i]
  90.             break
  91.     diairetis4 = numbers[check+1] / diairetis3
  92.     synexeia = 0
  93.  
  94.     if diairetis1 == diairetis3:
  95.         synexeia = diairetis2
  96.     if diairetis1 == diairetis4:
  97.         synexeia = diairetis2
  98.     if diairetis2 == diairetis3:
  99.         synexeia = diairetis1
  100.     if diairetis2 == diairetis4:
  101.         synexeia = diairetis1
  102.     wordNumbers.append(synexeia)
  103.  
  104.     # An eftasa sto telos
  105.     if check == length - 2:
  106.         if diairetis1 == diairetis3:
  107.             synexeia = diairetis1
  108.         if diairetis1 == diairetis4:
  109.             synexeia = diairetis1
  110.         if diairetis2 == diairetis3:
  111.             synexeia = diairetis2
  112.         if diairetis2 == diairetis4:
  113.             synexeia = diairetis2
  114.         wordNumbers.append(synexeia)
  115.  
  116. # Gia to TELEYTAIO NOUMERO, giati tha prepei na mpoun "length + 1" noumera
  117. # Tha tsekarw ton teleytaio arithmo tou numbers list: Tha ton sygkrinw me to teleytaio noumero ths listas wordNumbers
  118. diairetisLast1 = 0
  119. diairetisLast2 = 0
  120. for i in range(len(primes)):
  121.     if numbers[length-1] % primes[i] == 0:                  # An to last number tou numbers % me kapoion prime == 0
  122.         diairetisLast1 = primes[i]
  123.         diairetisLast2 = numbers[length-1] / diairetisLast1
  124.         break
  125. # Exw vrei tous 2 diairetes tou numbers[length-1]
  126. # Tha tsekarw poios apo tous 2 einai o last tis listas wordNumbers
  127. if diairetisLast1 == wordNumbers[len(wordNumbers)-1]:
  128.     wordNumbers.append(diairetisLast2)
  129. else:
  130.     wordNumbers.append(diairetisLast1)
  131.  
  132. print("  The products   : "  + str(wordNumbers))
  133.  
  134. # Create a list named "message", in which I append the letter coming from "alphabet" list, which is positioned in index
  135. message =[]
  136. for i in range(len(wordNumbers)):
  137.     message.append(alphabet[int(wordNumbers[i] - 1) % len(alphabet)])
  138.  
  139. print("Encrypted message: " + ''.join(map(str, message)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement