Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.52 KB | None | 0 0
  1. # 1.
  2. """
  3.  
  4. """
  5. import heapq
  6. def compress(s, step):
  7. compressed = ""
  8. index = 0
  9. count = 0
  10. before_bundle = None
  11. while (index + step) <= len(s):
  12. next_bundle = s[index:(index + step)]
  13.  
  14. if before_bundle is None:
  15. before_bundle = next_bundle
  16. count += 1
  17. index += step
  18. continue
  19.  
  20. if before_bundle == next_bundle:
  21. count += 1
  22. index += step
  23. continue
  24.  
  25. if count > 1:
  26. compressed += str(count)
  27. compressed += before_bundle
  28. else:
  29. index -= step
  30. compressed += s[index]
  31. index += 1
  32.  
  33. before_bundle = None
  34. count = 0
  35.  
  36. if count > 1:
  37. compressed += str(count)
  38. compressed += before_bundle
  39. else:
  40. index = index - step
  41.  
  42. compressed += s[index:]
  43.  
  44. return compressed
  45.  
  46.  
  47. def solution(s):
  48. lengths = []
  49. max_step = int(len(s) // 2)
  50.  
  51. for step in range(1, max_step+1):
  52. result = compress(s, step)
  53. heapq.heappush(lengths, len(result))
  54. return heapq.heappop(lengths)
  55.  
  56.  
  57. if __name__ == "__main__":
  58. print(solution("aabbaccc"))
  59. print(solution("abcabcdede"))
  60. print(solution("xababcdcdababcdcd"))
  61. print(solution("ababababababababababababababab")
  62. # 2
  63. """
  64.  
  65. """
  66. def get_balance_str(s):
  67. balance_str = []
  68. open_count = 0
  69. index = 0
  70. start_index = 0
  71.  
  72. while index < len(s):
  73. if s[index] == '(':
  74. open_count += 1
  75. else:
  76. open_count -= 1
  77.  
  78. if open_count == 0:
  79. balance_str.append(s[start_index:index + 1])
  80. start_index = index + 1
  81. balance_str.append(s[start_index:len(s)])
  82. break
  83. index += 1
  84. return balance_str
  85.  
  86.  
  87. def is_correct_str(balance):
  88. open_count = 0
  89. index = 0
  90. while index < len(balance):
  91. if balance[index] == '(':
  92. open_count += 1
  93. else:
  94. open_count -= 1
  95. if open_count < 0:
  96. return False
  97. index += 1
  98.  
  99. if open_count == 0:
  100. return True
  101. else:
  102. return False
  103.  
  104.  
  105. def reverse_bracket(s):
  106. result = ""
  107. for c in s:
  108. if c == '(':
  109. result += ')'
  110. else:
  111. result += '('
  112. return result
  113.  
  114.  
  115. def make(balance):
  116. result = ""
  117.  
  118. if not balance:
  119. return result
  120.  
  121. while True:
  122. u, v = get_balance_str(balance)
  123. if is_correct_str(u):
  124. result += u
  125. if v:
  126. balance = v
  127. else:
  128. break
  129. else:
  130. temp = "("
  131. temp += make(v)
  132. temp += ")"
  133. temp += reverse_bracket(u[1:-1])
  134. result += temp
  135. break
  136.  
  137. return result
  138.  
  139.  
  140. def solution(p):
  141. return make(p)
  142.  
  143. # 3
  144. """
  145.  
  146. """
  147. def is_find_hole(lock, except_x, except_y):
  148. start_x, end_x = except_x
  149. start_y, end_y = except_y
  150. for x, row in lock:
  151. for y, _ in row:
  152. if start_x <= x <= end_x and start_y <= y <= end_y:
  153. continue
  154. if lock[x][y] == 0:
  155. return True
  156. return False
  157.  
  158.  
  159. def rotate_right_key(key):
  160. M = len(key) - 1
  161. result = [[0 for _ in row] for row in key]
  162.  
  163. for row, tiles in enumerate(key):
  164. for column, tile in enumerate(tiles):
  165. result[column][M - row] = tile
  166.  
  167. return result
  168.  
  169.  
  170. def is_docked(key, lock):
  171. for x, row in enumerate(key):
  172. for y, _ in enumerate(row):
  173. if lock[x][y] == -1:
  174. continue
  175. if key[x][y] == lock[x][y]:
  176. return False
  177. return True
  178.  
  179.  
  180. def solution(key, lock):
  181. N = len(lock)
  182. M = len(key)
  183. keys = []
  184. for _ in range(4):
  185. keys.append(key)
  186. key = rotate_right_key(key)
  187.  
  188. # extend lock
  189. EXTEND_SIZE = N + M * 2 - 2
  190. extended_lock = [[-1 for _ in range(EXTEND_SIZE)] for _ in range(EXTEND_SIZE)]
  191.  
  192. for x in range(EXTEND_SIZE):
  193. for y in range(EXTEND_SIZE):
  194. if (M - 1) <= x <= (EXTEND_SIZE - M) and (M - 1) <= y <= (EXTEND_SIZE - M):
  195. extended_lock[x][y] = lock[x - (M - 1)][y - (M - 1)]
  196.  
  197. # resolve
  198. for i in range(EXTEND_SIZE - (M - 1)):
  199. for j in range(EXTEND_SIZE - (M - 1)):
  200. if is_find_hole(extended_lock, (j, j + M - 1), (i, i + M - 1)):
  201. continue
  202. tile = [[extended_lock[x][y] for y in range(j, j + M)] for x in range(i, i + M)]
  203. for key in keys:
  204. if is_docked(key, tile):
  205. return True
  206.  
  207. return False
  208.  
  209. # 4, 5, 6, 7 는 시간 부족으로 쳐다도 못봤습니다.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement