def get_binary_triangle_with_subset_order(size): num_elements = int((size + 1) * size / 2) elements = range(num_elements) tri_list = [util.get_all_zero_triangle(size)] for i in range(1, num_elements + 1): subset_list = itertools.combinations(elements, i) for subset in subset_list: data = [0] * num_elements for x in subset: data[x] = 1 triangle = [] start_idx = 0 for j in range(size): end_idx = start_idx + j + 1 triangle.append(data[start_idx:end_idx]) start_idx = start_idx + j + 1 tri_list.append(util.flip_triangle(triangle)) return tri_list
def stack_to_magog(stack): size = len(stack) magog = util.get_all_zero_triangle(size) print('CONVERTING') util.print_array(stack) for idx in range(size): stack_row = stack[size - 1 - idx] start_idx = idx mrow_idx = size - 1 for i in reversed(range(len(stack_row))): if stack_row[i] == 0: start_idx += 1 else: mrow_len = len(magog[mrow_idx]) for j in range(start_idx, mrow_len): magog[mrow_idx][j] = magog[mrow_idx][j] + 1 mrow_idx += -1 print('------ idx=', idx) util.print_array(magog) return magog
def get_down_for_cliff(B): size = len(B) D = util.get_all_zero_triangle(size) for k in range(size): #logging.debug('B[k]', k, B[k]) D[k][k] = k + 1 - sum([B[k][j] for j in range(k + 1)]) return D
def to_BD(triangle): size = len(triangle) B = util.get_all_zero_triangle(size) D = util.get_all_zero_triangle(size) tiered = [[x + idx for x in row] for idx, row in enumerate(triangle)] path_triangle = [[size - x for x in reversed(row)] for row in reversed(tiered)] #util.print_array(path_triangle) #tiered = [ [size - x - idx for x in row] for idx,row in enumerate(triangle)] #path_triangle = [[x for x in reversed(row)] for row in reversed(tiered)] #path_triangle = [ [size - x - idx for x in reversed(row)] for idx,row in enumerate(triangle)] #path_triangle = [x for x in reversed(path_triangle)] #print('ballot') #util.print_array(triangle) #print('path triangle') #util.print_array(path_triangle) for row_idx, row in enumerate(path_triangle): if row[0] == row_idx: B[row_idx][0] = 1 for col_idx in range(1, len(row)): if row[col_idx] < row[col_idx - 1]: B[row_idx][col_idx] = 1 D[row_idx][-1] = row[-1] for col_idx in range(0, len(row) - 1): if row[col_idx] > row[col_idx + 1] + 1: D[row_idx][col_idx] = row[col_idx] - row[col_idx + 1] - 1 #print('binary') #util.print_array(B) #print('down') #util.print_array(D) return B, D
def to_sst(columns): size = len(columns) triangle = util.get_all_zero_triangle(size) for idx,x in enumerate(reversed(columns)): if x > 0: triangle[idx][x-1] = 1 return triangle
def comb(binary_triangle): size = len(binary_triangle) down_triangle = util.get_all_zero_triangle(len(binary_triangle)) for k in reversed(range(size)): down_triangle[k][k] = k + 1 - sum( [binary_triangle[k][j] for j in range(k + 1)]) return comb_BD(binary_triangle, down_triangle)
def binary_for_layer(layer, tri_size): binlayer_tri = util.get_all_zero_triangle(tri_size) binlayer_tri = [ row for row in reversed(binlayer_tri)] for idx,x in enumerate(layer): if x > 0: binlayer_tri[x-1][idx]=1 rev_tri = [ x for x in reversed(binlayer_tri)] layer_size = len(layer) # return the subtriangle for top layering rev_tri = rev_tri[0: layer_size] return rev_tri
def tip_west(triangle): size = len(triangle) tip_triangle = util.get_all_zero_triangle(size) tip_triangle = [] for row_idx in range(size): tip_row = [] for col_idx in range(size - row_idx): count = 0 for i in range(size - row_idx - col_idx): if triangle[row_idx][i] > col_idx: count += 1 tip_row.append(count) tip_triangle.append(tip_row) return tip_triangle
def word_to_triangle(word): size = len(word) tri = util.get_all_zero_triangle(size) for idx, w in enumerate(word): if w > 0: tri[size - w][idx] = 1 for idx in range(size): if sum(tri[idx]) == 0: del tri[idx] for k in range(idx): tri[k].append(0) tri.insert(0, [ 0, ]) return tri