Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- COUNT_PARAM = 2
- START_POSITION_PYRAMID = 0
- START_STEP = 1
- MIN_SIZE_PYRAMID = 1
- STEP = 2
- FIRST_PART_PYRAMID = 1
- SECOND_PART_PYRAMID = 3
- rules = {}
- def init():
- rules['0000'] = '0000'
- rules['0001'] = '1000'
- rules['0010'] = '0001'
- rules['0011'] = '0010'
- rules['0100'] = '0000'
- rules['0101'] = '0010'
- rules['0110'] = '1011'
- rules['0111'] = '1011'
- rules['1000'] = '0100'
- rules['1001'] = '0101'
- rules['1010'] = '0111'
- rules['1011'] = '1111'
- rules['1100'] = '1101'
- rules['1101'] = '1110'
- rules['1110'] = '0111'
- rules['1111'] = '1111'
- def make_list(str):
- return [i for i in str]
- def make_string_from_list(lst):
- return ''.join(lst)
- def check_parity(num):
- if num % 2 == 0:
- return True
- return False
- def check_identity(pyramid):
- value = pyramid[0]
- for it in pyramid:
- if it != value:
- return False
- return True
- def next_transition_rule(current_triangle):
- current_triangle = reverse_list(current_triangle)
- return reverse_list(make_list(rules[make_string_from_list(current_triangle)]))
- def reverse_list(lst):
- return lst[::-1]
- def get_new_triangle(pyramid, pointer_one, pointer_two):
- current_triangle = []
- if check_parity(pointer_one) or pointer_one == 0: # 0 is exclusion
- current_triangle += pyramid[pointer_one:pointer_one + FIRST_PART_PYRAMID:]
- current_triangle += pyramid[pointer_two:pointer_two + SECOND_PART_PYRAMID:]
- pointer_one += FIRST_PART_PYRAMID
- pointer_two += SECOND_PART_PYRAMID
- else:
- current_triangle += pyramid[pointer_one:pointer_one + SECOND_PART_PYRAMID:]
- current_triangle += pyramid[pointer_two:pointer_two + FIRST_PART_PYRAMID:]
- pointer_one += SECOND_PART_PYRAMID
- pointer_two += FIRST_PART_PYRAMID
- return current_triangle, pointer_one, pointer_two
- def set_new_triangle(pyramid, new_pyramid, pointer_one, pointer_two):
- current_triangle = []
- if check_parity(pointer_one) or pointer_one == 0: # 0 is exclusion
- current_triangle += pyramid[pointer_one:pointer_one + FIRST_PART_PYRAMID:]
- current_triangle += pyramid[pointer_two:pointer_two + SECOND_PART_PYRAMID:]
- current_triangle = next_transition_rule(current_triangle)
- for it in range(pointer_one, pointer_one + FIRST_PART_PYRAMID):
- new_pyramid[it] = current_triangle[it - pointer_one]
- for it in range(pointer_two, pointer_two + SECOND_PART_PYRAMID):
- new_pyramid[it] = current_triangle[it - pointer_two + FIRST_PART_PYRAMID]
- pointer_one += FIRST_PART_PYRAMID
- pointer_two += SECOND_PART_PYRAMID
- else:
- current_triangle += pyramid[pointer_one:pointer_one + SECOND_PART_PYRAMID:]
- current_triangle += pyramid[pointer_two:pointer_two + FIRST_PART_PYRAMID:]
- current_triangle = next_transition_rule(current_triangle)
- for it in range(pointer_one, pointer_one + SECOND_PART_PYRAMID):
- new_pyramid[it] = current_triangle[it - pointer_one]
- for it in range(pointer_two, pointer_two + FIRST_PART_PYRAMID):
- new_pyramid[it] = current_triangle[it - pointer_two + SECOND_PART_PYRAMID]
- pointer_one += SECOND_PART_PYRAMID
- pointer_two += FIRST_PART_PYRAMID
- return new_pyramid, pointer_one, pointer_two
- def transformation_pyramid(pyramid):
- new_pyramid = range(len(pyramid))
- position_pyramid = START_POSITION_PYRAMID
- current_size_step = START_STEP
- while position_pyramid < len(pyramid):
- pointer_one = position_pyramid
- pointer_two = position_pyramid + current_size_step
- while pointer_one < position_pyramid + current_size_step:
- new_pyramid, pointer_one, pointer_two = set_new_triangle(pyramid, new_pyramid, pointer_one, pointer_two)
- position_pyramid += current_size_step * 2 + STEP
- current_size_step += STEP * 2
- return new_pyramid
- def reduce_pyramid(pyramid):
- if len(pyramid) <= MIN_SIZE_PYRAMID:
- return pyramid, False
- reduce_is_ok = True
- new_pyramid = []
- position_pyramid = START_POSITION_PYRAMID
- current_size_step = START_STEP
- while position_pyramid < len(pyramid):
- pointer_one = position_pyramid
- pointer_two = position_pyramid + current_size_step
- while pointer_one < position_pyramid + current_size_step:
- current_triangle, pointer_one, pointer_two = get_new_triangle(pyramid, pointer_one, pointer_two)
- if check_identity(current_triangle) is False:
- reduce_is_ok = False
- return pyramid, reduce_is_ok
- new_pyramid.append(current_triangle.pop())
- position_pyramid += current_size_step * 2 + STEP
- current_size_step += STEP * 2
- return new_pyramid, reduce_is_ok
- def main_algorithm(pyramid):
- while len(pyramid) > MIN_SIZE_PYRAMID:
- reduce_is_ok = True
- counter = 0
- while reduce_is_ok:
- pyramid, reduce_is_ok = reduce_pyramid(pyramid)
- counter += 1
- if counter > 1:
- print(make_string_from_list(reverse_list(pyramid)))
- if len(pyramid) > MIN_SIZE_PYRAMID:
- pyramid = transformation_pyramid(pyramid)
- print(make_string_from_list(reverse_list(pyramid)))
- if __name__ == "__main__":
- init()
- if len(sys.argv) != COUNT_PARAM:
- raise ("incorrect script parameters")
- bynary_string = sys.argv[1]
- main_algorithm(make_list(reverse_list(bynary_string)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement