daily pastebin goal
19%
SHARE
TWEET

Untitled

a guest Mar 23rd, 2019 88 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. from collections import defaultdict
  2. from math import gcd
  3. from collections import Counter
  4. from statistics import mean
  5. import os
  6.  
  7.  
  8. word_len = 20
  9. key_len = 20
  10.  
  11. with open(os.path.join('data', 'case8'), encoding='cp1251') as s:
  12.     txt = s.read()
  13.  
  14. substr = defaultdict(list)
  15. for l in range(3, word_len + 1):
  16.     for i in range(len(txt) - l):
  17.         s = txt[i:i + l]
  18.         substr[s].append(i)
  19. l_k1 = set()
  20. for x in substr:
  21.     m = substr[x]
  22.     i = 0
  23.     gcd_x = 0
  24.     for j in range(1, len(m)):
  25.         if m[j] - m[i] >= len(x):
  26.             gcd_x = gcd(m[j] - m[i], gcd_x)
  27.             i = j
  28.     if gcd_x:
  29.         l_k1.add(gcd_x)
  30. print(list(sorted(l_k1))[:key_len + 1])
  31.  
  32. I = lambda s: sum(x[1] * (x[1] - 1) / len(s) / (len(s) - 1)
  33.                   for x in Counter(s).most_common())
  34. means = [mean(I(txt[i::l]) for i in range(l))
  35.         for l in range(2, key_len + 1)]
  36. l_k2 = [(abs(0.0553 - means[i]), i + 2) for i in range(len(means))]
  37. l_k2.sort()
  38. _, l_k2 = zip(*l_k2)
  39. print(l_k2)
  40.  
  41. l_k = [x for x in l_k2 if x in l_k1]
  42. print(l_k)
  43.  
  44. k_len = 4
  45. k = []
  46. alp = ''.join(map(chr, range(ord('А'), ord('Я') + 1))) + ' '
  47. get_ord = alp.find
  48. get_chr = alp.__getitem__
  49. for i in range(k_len):
  50.     c = Counter(txt[i::k_len]).most_common(1)[0][0]
  51.     k.append((get_ord(c) - get_ord(' ') + len(alp)) % len(alp))
  52. txt = ''.join(get_chr((get_ord(x) - k[i % k_len] + len(alp)) % len(alp))
  53.                 for i, x in enumerate(txt))
  54. print(txt)
  55. print(k)
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top