Advertisement
Guest User

Untitled

a guest
Nov 20th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.57 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # coding: utf8
  3.  
  4. CIPHER = [
  5. u'EKTAX', u'CEMEA', u'NVHEH', u'NEKLT', u'LUEEN', u'SAFUA', u'NTNTD', u'NDRDA',
  6. u'UBGES', u'IVLUR', u'UNTEA', u'IPPRN', u'TAEEE', u'RDROS', u'ILRRT', u'HNÄHA',
  7. u'BTNFV', u'BVETR', u'VLRDP', u'SUEUS', u'HIÄLI', u'GUAEO', u'HLVLE', u'TXFIE',
  8. u'HXDVD', u'NFNÄF', u'AX'
  9. ]
  10.  
  11. ALPHA_UC = u'ABCDEFGHIJKLMNOPQRSTUVWXYZÅÄÖ'
  12.  
  13. """
  14. EE = 3 (E = L | E = T)
  15. PP = 1
  16. RR = 1
  17. """
  18.  
  19. KEY = {
  20. u'E':u'S', # 17
  21. u'N':u'F', # 12
  22. u'A':u'L', # 10
  23. u'T':u'G', # 10
  24. u'R':u'P', # 9
  25. u'L':u'U', # 8
  26. u'U':u'E', # 8
  27. u'H':u'I', # 7
  28. u'V':u'L', # 7
  29. u'D':u'O', # 7
  30. u'I':u'A', # 6
  31. u'F':u'D', # 5
  32. u'X':u'K', # 4
  33. u'B':u'H', # 3
  34. u'Ä':u'T', # 3
  35. u'P':u'R', # 3
  36. u'K':u'M', # 2
  37. u'O':u'N', # 2
  38. u'G':u'C', # 2
  39. u'C':u'Ö', # 1
  40. u'M':u'Ä', # 1
  41. u'J':u'Å',
  42. u'Q':u'B',
  43. u'S':u'J',
  44. u'Y':u'Y',
  45. u'Z':u'X',
  46. u'Å':u'Z',
  47. u'Ö':u'Q',
  48. }
  49.  
  50. def freqs(c,n=1) :
  51. fd={}
  52. for i in range(len(c)) :
  53. a = c[i:i+n]
  54. if len(a) < n : continue
  55. if a in fd.keys() :
  56. fd[a] += 1
  57. else :
  58. fd[a] = 1
  59. return fd
  60.  
  61. def substitute(k, c) :
  62. ns = u''
  63. for a in c :
  64. if a in k.keys() :
  65. ns += k[a]
  66. else :
  67. ns += a
  68. return ns
  69.  
  70. def rotate(k, c, alpha) :
  71. ns = u''
  72. for a in c :
  73. ns += alpha[ (alpha.index(a)+k) % len(alpha) ]
  74. return ns
  75.  
  76. def __idxs(s,v) :
  77. """ Gets the indicies in s where v occurs """
  78. idxs=[]
  79. for i in range(len(s)) :
  80. if s[i] == v : idxs.append(i)
  81. return idxs
  82. def __matches(s,v,il) :
  83. """ Returns true if s[i] == v for i in il """
  84. for i in il :
  85. if s[i] != v : return False
  86. return True
  87. def __matchpattern(a,b) :
  88. assert len(a) == len(b), 'Non-matching lengths'
  89. memA = []
  90. memB = []
  91. for i in range(len(a)) :
  92. if a[i] in memA : # non-unique
  93. il = __idxs(memA, a[i])
  94. if not __matches(memB, b[i], il) :
  95. return False
  96. else : # unique
  97. if b[i] in memB :
  98. return False
  99. memA.append(a[i])
  100. memB.append(b[i])
  101. return True
  102.  
  103. def wordfinder(c, w) :
  104. """ Extract all possible substrings """
  105. substrs=[]
  106. for i in range(len(c)) :
  107. if len(c[i:i+len(w)]) == len(w) :
  108. substrs.append(c[i:i+len(w)])
  109. """ Match all substrings """
  110. matches=[]
  111. for substr in substrs :
  112. if __matchpattern(w, substr) :
  113. matches.append(substr)
  114. return matches
  115.  
  116. ciphertext = ''.join(CIPHER)
  117. print(wordfinder(ciphertext, 'UPPGIFT'))
  118.  
  119. #for i in range(len(ALPHA_UC)*2) :
  120. # print(rotate(i, ciphertext, list(ALPHA_UC)))
  121. # print('-----------------------------')
  122. #ciphertext = substitute(KEY, ciphertext)
  123. #print(ciphertext)
  124.  
  125. #print(wordfinder(ciphertext, 'LÖST'))
  126. #fd = freqs(ciphertext, n=1)
  127. #for k,v in zip(fd.keys(), fd.values()) :
  128. # print(u'%s => %d' % (k,v))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement