Advertisement
Guest User

Untitled

a guest
May 25th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.58 KB | None | 0 0
  1. import sys
  2.  
  3. COUNT_PARAM = 2
  4. START_POSITION_PYRAMID = 0
  5. START_STEP = 1
  6. MIN_SIZE_PYRAMID = 1
  7. STEP = 2
  8. FIRST_PART_PYRAMID = 1
  9. SECOND_PART_PYRAMID = 3
  10.  
  11. rules = {}
  12.  
  13.  
  14. def init():
  15. rules['0000'] = '0000'
  16. rules['0001'] = '1000'
  17. rules['0010'] = '0001'
  18. rules['0011'] = '0010'
  19. rules['0100'] = '0000'
  20. rules['0101'] = '0010'
  21. rules['0110'] = '1011'
  22. rules['0111'] = '1011'
  23. rules['1000'] = '0100'
  24. rules['1001'] = '0101'
  25. rules['1010'] = '0111'
  26. rules['1011'] = '1111'
  27. rules['1100'] = '1101'
  28. rules['1101'] = '1110'
  29. rules['1110'] = '0111'
  30. rules['1111'] = '1111'
  31.  
  32.  
  33. def make_list(str):
  34. return [i for i in str]
  35.  
  36.  
  37. def make_string_from_list(lst):
  38. return ''.join(lst)
  39.  
  40.  
  41. def check_parity(num):
  42. if num % 2 == 0:
  43. return True
  44. return False
  45.  
  46.  
  47. def check_identity(pyramid):
  48. value = pyramid[0]
  49. for it in pyramid:
  50. if it != value:
  51. return False
  52. return True
  53.  
  54.  
  55. def next_transition_rule(current_triangle):
  56. current_triangle = reverse_list(current_triangle)
  57. return reverse_list(make_list(rules[make_string_from_list(current_triangle)]))
  58.  
  59.  
  60. def reverse_list(lst):
  61. return lst[::-1]
  62.  
  63.  
  64. def get_new_triangle(pyramid, pointer_one, pointer_two):
  65. current_triangle = []
  66. if check_parity(pointer_one) or pointer_one == 0: # 0 is exclusion
  67. current_triangle += pyramid[pointer_one:pointer_one + FIRST_PART_PYRAMID:]
  68. current_triangle += pyramid[pointer_two:pointer_two + SECOND_PART_PYRAMID:]
  69. pointer_one += FIRST_PART_PYRAMID
  70. pointer_two += SECOND_PART_PYRAMID
  71. else:
  72. current_triangle += pyramid[pointer_one:pointer_one + SECOND_PART_PYRAMID:]
  73. current_triangle += pyramid[pointer_two:pointer_two + FIRST_PART_PYRAMID:]
  74. pointer_one += SECOND_PART_PYRAMID
  75. pointer_two += FIRST_PART_PYRAMID
  76.  
  77. return current_triangle, pointer_one, pointer_two
  78.  
  79.  
  80. def set_new_triangle(pyramid, new_pyramid, pointer_one, pointer_two):
  81. current_triangle = []
  82. if check_parity(pointer_one) or pointer_one == 0: # 0 is exclusion
  83. current_triangle += pyramid[pointer_one:pointer_one + FIRST_PART_PYRAMID:]
  84. current_triangle += pyramid[pointer_two:pointer_two + SECOND_PART_PYRAMID:]
  85.  
  86. current_triangle = next_transition_rule(current_triangle)
  87.  
  88. for it in range(pointer_one, pointer_one + FIRST_PART_PYRAMID):
  89. new_pyramid[it] = current_triangle[it - pointer_one]
  90.  
  91. for it in range(pointer_two, pointer_two + SECOND_PART_PYRAMID):
  92. new_pyramid[it] = current_triangle[it - pointer_two + FIRST_PART_PYRAMID]
  93.  
  94. pointer_one += FIRST_PART_PYRAMID
  95. pointer_two += SECOND_PART_PYRAMID
  96. else:
  97. current_triangle += pyramid[pointer_one:pointer_one + SECOND_PART_PYRAMID:]
  98. current_triangle += pyramid[pointer_two:pointer_two + FIRST_PART_PYRAMID:]
  99.  
  100. current_triangle = next_transition_rule(current_triangle)
  101.  
  102. for it in range(pointer_one, pointer_one + SECOND_PART_PYRAMID):
  103. new_pyramid[it] = current_triangle[it - pointer_one]
  104.  
  105. for it in range(pointer_two, pointer_two + FIRST_PART_PYRAMID):
  106. new_pyramid[it] = current_triangle[it - pointer_two + SECOND_PART_PYRAMID]
  107.  
  108. pointer_one += SECOND_PART_PYRAMID
  109. pointer_two += FIRST_PART_PYRAMID
  110.  
  111. return new_pyramid, pointer_one, pointer_two
  112.  
  113.  
  114. def transformation_pyramid(pyramid):
  115. new_pyramid = range(len(pyramid))
  116. position_pyramid = START_POSITION_PYRAMID
  117. current_size_step = START_STEP
  118. while position_pyramid < len(pyramid):
  119. pointer_one = position_pyramid
  120. pointer_two = position_pyramid + current_size_step
  121. while pointer_one < position_pyramid + current_size_step:
  122. new_pyramid, pointer_one, pointer_two = set_new_triangle(pyramid, new_pyramid, pointer_one, pointer_two)
  123. position_pyramid += current_size_step * 2 + STEP
  124. current_size_step += STEP * 2
  125.  
  126. return new_pyramid
  127.  
  128.  
  129. def reduce_pyramid(pyramid):
  130. if len(pyramid) <= MIN_SIZE_PYRAMID:
  131. return pyramid, False
  132. reduce_is_ok = True
  133. new_pyramid = []
  134. position_pyramid = START_POSITION_PYRAMID
  135. current_size_step = START_STEP
  136. while position_pyramid < len(pyramid):
  137. pointer_one = position_pyramid
  138. pointer_two = position_pyramid + current_size_step
  139. while pointer_one < position_pyramid + current_size_step:
  140. current_triangle, pointer_one, pointer_two = get_new_triangle(pyramid, pointer_one, pointer_two)
  141.  
  142. if check_identity(current_triangle) is False:
  143. reduce_is_ok = False
  144. return pyramid, reduce_is_ok
  145.  
  146. new_pyramid.append(current_triangle.pop())
  147.  
  148. position_pyramid += current_size_step * 2 + STEP
  149. current_size_step += STEP * 2
  150.  
  151. return new_pyramid, reduce_is_ok
  152.  
  153.  
  154. def main_algorithm(pyramid):
  155. while len(pyramid) > MIN_SIZE_PYRAMID:
  156. reduce_is_ok = True
  157. counter = 0
  158. while reduce_is_ok:
  159. pyramid, reduce_is_ok = reduce_pyramid(pyramid)
  160. counter += 1
  161. if counter > 1:
  162. print(make_string_from_list(reverse_list(pyramid)))
  163. if len(pyramid) > MIN_SIZE_PYRAMID:
  164. pyramid = transformation_pyramid(pyramid)
  165. print(make_string_from_list(reverse_list(pyramid)))
  166.  
  167.  
  168. if __name__ == "__main__":
  169.  
  170. init()
  171.  
  172. if len(sys.argv) != COUNT_PARAM:
  173. raise ("incorrect script parameters")
  174.  
  175. bynary_string = sys.argv[1]
  176. main_algorithm(make_list(reverse_list(bynary_string)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement