Advertisement
UF6

3.1 Problem 1 and 3

UF6
Jul 5th, 2016
390
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.62 KB | None | 0 0
  1. def encode(n, seq):
  2.     its = [iter(seq)]*n
  3.     return zip(*its)
  4.  
  5. def Playfair(s):
  6.     keystring = []
  7.     for j in s+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
  8.         if j not in keystring:
  9.             keystring.append(j)
  10.     return list(take(5, keystring))
  11.  
  12. print (Playfair("EVENTHOUGHTHECOLDWARHASCOMEANDGONEITSCHILLWINDSSTILLBLOW"))
  13. #[('E', 'V', 'N', 'T', 'H'), ('O', 'U', 'G', 'C', 'L'), ('D', 'W', 'A', 'R', 'S'), ('M', 'I', 'B', 'F', 'J'), ('K', 'P', 'Q', 'X', 'Y')]
  14.  
  15. def encode(n, seq):
  16.     its = [iter(seq)]*n
  17.     return zip(*its)
  18.  
  19. def Playfair(s):
  20.     keystring = []
  21.     for j in s+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
  22.         if j not in keystring:
  23.             keystring.append(j)
  24.     return list(take(5, keystring))
  25. print (Playfair("All those who believe in psychokinesis raise my hand"))
  26.  
  27. #[('A', 'l', ' ', 't', 'h'), ('o', 's', 'e', 'w', 'b'), ('i', 'v', 'n', 'p', 'y'), ('c', 'k', 'r', 'a', 'm'), ('d', 'B', 'C', 'D', 'E'), ('F', 'G', 'H', 'I', 'J'), ('K', 'L', 'M', 'N', 'O'), ('P', 'Q', 'R', 'S', 'T'), ('U', 'V', 'W', 'X', 'Y')]
  28.  
  29.  
  30. alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  31. len(alphabet)
  32. #26
  33. import random
  34. alpha2 = []
  35. for i in alphabet: alpha2.append(i)
  36. playFair = []
  37. row = []
  38. while(len(alpha2))>1:
  39.     index = random.randint(0,len(alpha2)-1)
  40.     row.append(alpha2[index])
  41.     alpha2.remove(alpha2[index])
  42.     if len(row) == 5:
  43.         playFair.append(row)
  44.         row = []
  45. for row in playFair:
  46.     print(row)
  47.  
  48. print("left out letter: ", alpha2)
  49.  
  50. #['G', 'R', 'X', 'M', 'F']
  51. #['J', 'H', 'S', 'V', 'O']
  52. #['U', 'W', 'L', 'D', 'Z']
  53. #['I', 'C', 'P', 'A', 'B']
  54. #['N', 'T', 'E', 'Q', 'Y']
  55. #left out letter:  ['K']
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement