Guest User

Untitled

a guest
May 25th, 2018
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.27 KB | None | 0 0
  1. # -*- coding: utf-8 -*-
  2. import sys
  3.  
  4. lookback = "a" * 2703
  5. front = sys.stdin.read()
  6. result = ""
  7.  
  8. lookup = {0: " ", 1: "a", 2: "b", 3: "c", 4: "d", 5: "e", 6: "f", 7: "g", 8: "h",
  9. 9: "i", 10: "j", 11: "k", 12: "l", 13: "m", 14: "n", 15: "o", 16: "p",
  10. 17: "q", 18: "r", 19: "s", 20: "t", 21: "u", 22: "v", 23: "w", 24: "x",
  11. 25: "y", 26: "z", 27: "A", 28: "B", 29: "C", 30: "D", 31: "E", 32: "F",
  12. 33: "G", 34: "H", 35: "I", 36: "J", 37: "K", 38: "L", 39: "M", 40: "N",
  13. 41: "O", 42: "P", 43: "Q", 44: "R", 45: "S", 46: "T", 47: "U", 48: "V",
  14. 49: "W", 50: "X", 51: "Y", 52: "Z"}
  15.  
  16. def cmp(a, b):
  17. if a[1] == b[1]:
  18. return 0
  19. if a[1] > b[1]:
  20. return 1
  21. return -1
  22.  
  23. freq = {' ': 725, 'B': 79, 'D': 66, 'F': 51, 'H': 81, 'J': 58, 'L': 65, 'N': 86, 'P': 80, 'R': 59, 'T': 93, 'V': 78, 'X': 98, 'Z': 47, 'b': 133, 'd': 240, 'f': 445, 'h': 374, 'j': 91, 'l': 311, 'n': 346, 'p': 291, 'r': 398, 't': 470, 'v': 118, 'x': 96, 'z': 56, '%': 1347, 'A': 65, 'C': 67, 'E': 72, 'G': 63, 'I': 89, 'K': 45, 'M': 55, 'O': 58, 'Q': 58, 'S': 89, 'U': 67, 'W': 79, 'Y': 23, 'a': 459, 'c': 228, 'e': 1155, 'g': 322, 'i': 508, 'k': 130, 'm': 187, 'o': 408, 'q': 73, 's': 421, 'u': 215, 'w': 168, 'y': 152}
  24.  
  25. codes = {" ": "", "a": "", "b": "", "c": "", "d": "", "e": "", "f": "", "g": "", "h": "", "i": "", "j": "", "k": "", "l": "", "m": "", "n": "", "o": "", "p": "", "q": "", "r": "", "s": "", "t": "", "u": "", "v": "", "w": "", "x": "", "y": "", "z": "", "A": "", "B": "", "C": "", "D": "", "E": "", "F": "", "G": "", "H": "", "I": "", "J": "", "K": "", "L": "", "M": "", "N": "", "O": "", "P": "", "Q": "", "R": "", "S": "", "T": "", "U": "", "V": "", "W": "", "X": "", "Y": "", "Z": "", "%": ""}
  26.  
  27. encLookup = {}
  28.  
  29. def genCodes():
  30. codes = {" ": "", "a": "", "b": "", "c": "", "d": "", "e": "", "f": "", "g": "", "h": "", "i": "", "j": "", "k": "", "l": "", "m": "", "n": "", "o": "", "p": "", "q": "", "r": "", "s": "", "t": "", "u": "", "v": "", "w": "", "x": "", "y": "", "z": "", "A": "", "B": "", "C": "", "D": "", "E": "", "F": "", "G": "", "H": "", "I": "", "J": "", "K": "", "L": "", "M": "", "N": "", "O": "", "P": "", "Q": "", "R": "", "S": "", "T": "", "U": "", "V": "", "W": "", "X": "", "Y": "", "Z": "", "%": ""}
  31. #pirnt codes
  32. #pirnt "gen codes"
  33. q = [([k], v) for (k, v) in sorted(freq.items(), cmp)]
  34. while len(q) > 1:
  35. a = q[0]
  36. b = q[1]
  37.  
  38. for i in a[0]:
  39. codes[i] += "0"
  40. for i in b[0]:
  41. codes[i] += "1"
  42.  
  43. q[1] = (a[0] + b[0], a[1] + b[1])
  44. q = sorted(q[1:], cmp)
  45. #pirnt codes
  46. return dict([(k, v[::-1]) for (k, v) in codes.items()])
  47.  
  48.  
  49. reverseLookup = dict([(v, k) for (k, v) in lookup.items()])
  50.  
  51. def stringFromLocation(location):
  52. return lookup[location]
  53.  
  54. def locationFromString(string):
  55. return reverseLookup[string]
  56.  
  57. # 54 => ab
  58. def stringFromLocation2(location):
  59. string = ""
  60. subtract = 0
  61.  
  62. string += lookup[((location - (location % 53)) / 53) ]
  63. string += lookup[location % 53]
  64.  
  65. return string
  66.  
  67. # ab => 53
  68. def locationFromString2(string):
  69. return reverseLookup[string[0]] * 53 + reverseLookup[string[1]]
  70.  
  71. while len(front) > 0:
  72. #print "main loop"
  73. a = 52
  74. found = False
  75. pieceLengthCode = ""
  76. piecePositionCode = ""
  77. pieceLength = 0
  78. if len(front) > 4:
  79. while a > 4 and not found:
  80. if len(front) >= a:
  81. #print "look loop"
  82. piece = front[:a]
  83.  
  84. if lookback.find(piece) != -1:
  85. location = lookback.find(piece)
  86.  
  87. #print piece, len(piece)
  88. found = True
  89. pieceLength = len(piece)
  90. #print pieceLength == len(piece)
  91. piecePositionCode = stringFromLocation2(location)
  92. pieceLengthCode = stringFromLocation(a)
  93.  
  94. a -= 1
  95.  
  96. if found:
  97. #print "found"
  98. #print pieceLength
  99. result += ("%" + piecePositionCode + pieceLengthCode)
  100. #print result
  101. #print lookback
  102. lookback = (lookback + front[:pieceLength])[-2703:]
  103. #print lookback
  104. #print front
  105. if len(front) == pieceLength:
  106. front = ""
  107. else:
  108. front = front[-(len(front) - pieceLength):]
  109. #print front
  110.  
  111. else:
  112. #print "not found"
  113. #print result
  114. #print front
  115. result += front[0]
  116. #print front[0]
  117. #print result
  118. lookback = (lookback + front[0])[-2703:]
  119. if len(front) == 1:
  120. front = ""
  121. else:
  122. front = front[-(len(front) - 1):]
  123.  
  124. found = False
  125.  
  126. #print result
  127.  
  128. firstStage = result
  129. result = ""
  130.  
  131. for c in firstStage:
  132. encLookup = genCodes()
  133. result += encLookup[c]
  134. freq[c] += 1
  135.  
  136. sys.stdout.write(result)
Add Comment
Please, Sign In to add comment