def calk_wil_tree_for_one_level(prev_list_at_level, display_option): list_at_level= copy.deepcopy(prev_list_at_level) # print list_at_level list_len = len(list_at_level) # print 'list_len = ', list_len to_be_inserted_list = [] for i in range(list_len): l_child = [0, 0] l_child[0] = list_at_level[i][0] l_child[1] = list_at_level[i][0] + list_at_level[i][1] to_be_inserted_list.append(l_child) r_child = [0, 0] r_child[0] = list_at_level[i][0] + list_at_level[i][1] r_child[1] = list_at_level[i][1] to_be_inserted_list.append(r_child) # print to_be_inserted_list # print list_at_level if(display_option): print_fare_fraction_list(to_be_inserted_list) print " " return to_be_inserted_list
def fare_plus_fare(n): fare_list = get_fare_list(n) print "=================================" print_fare_fraction_list(fare_list) fare_list_len = len(fare_list) fare_plus_fare_list = [] for i in range(fare_list_len): for j in range(fare_list_len): a1 = fare_list[i][0] b1 = fare_list[i][1] a2 = fare_list[j][0] b2 = fare_list[j][1] c, d = add_two_fract(a1, b1, a2, b2) tmp = [c, d] fare_plus_fare_list.append(tmp) fare_plus_fare_list_len = len(fare_plus_fare_list) print "\n=== ==============================================" print_fare_fraction_list(fare_plus_fare_list) print "\n=== fare_list_len = %d, fare_plus_fare_list_len = %d,"%(fare_list_len, fare_plus_fare_list_len) return fare_plus_fare_list
def ster_broc_tree(prev_list_at_level, display_option): list_at_level= copy.deepcopy(prev_list_at_level) # print list_at_level list_len = len(list_at_level) to_be_inserted_list = [] for i in range(list_len - 1): tmp = [0, 0] tmp[0] = list_at_level[i][0] + list_at_level[i + 1][0] tmp[1] = list_at_level[i][1] + list_at_level[i + 1][1] to_be_inserted_list.append(tmp) # print to_be_inserted_list # interleave to_be_inserted_list with list_at_level # Note, to_be_insert_list is one item less than list_at_level. # And they are both sorted. # Since both are lists which can have many items, it is important # to have an effective way to interleave those two data. # For example, if we just use normal list insert function. # It can be very slow. # Method_1:, we use extended slice syntax in Python. # start_1 = time.time() new_list = list_at_level + to_be_inserted_list new_list[::2] = list_at_level new_list[1::2] = to_be_inserted_list # end_1 = time.time() # print "method_1: ", (end_1 - start_1) ''' # Method_2: start_2 = time.time() new_list_2 = [val for pair in zip(list_at_level , list_at_level ) for val in pair] end_2 = time.time() print "method_2: ", (end_2 - start_2) # Method_3: start_3 = time.time() for i in range(len(to_be_inserted_list)): tmp = to_be_inserted_list[i] list_at_level.insert(2*i + 1, tmp) end_3 = time.time() print "method_3: ", (end_3 - start_3) ''' # print list_at_level half_len = len(new_list)/2 + 1 if(display_option): print_fare_fraction_list(new_list[:half_len ]) print " " return new_list, to_be_inserted_list
def fare_plus_fare_sorted_by_deno(fare_plus_fare_list): fare_plus_fare_2d_arr = np.asarray(fare_plus_fare_list) print fare_plus_fare_2d_arr print "!!!!!!!!!!!!! The following sorting function is WRONG !!!!!!!!!!!!!!!!!!1" print "\n===================== sorted = axis = 0=========================" fare_plus_fare_sorted_by_denom_2d_arr = np.sort(fare_plus_fare_2d_arr, axis=0) print fare_plus_fare_sorted_by_denom_2d_arr print_fare_fraction_list(fare_plus_fare_sorted_by_denom_2d_arr) print "\n===================== sorted = axis = 1=========================" fare_plus_fare_sorted_by_denom_2d_arr = np.sort(fare_plus_fare_2d_arr, axis=1) print fare_plus_fare_sorted_by_denom_2d_arr print_fare_fraction_list(fare_plus_fare_sorted_by_denom_2d_arr) return fare_plus_fare_sorted_by_denom_2d_arr
def extract_diagonal_from_calk_wil_tree(tree_as_breadth_1st_trav_list_upto_level, diagonal, display_option = True): list_for_diagonal = [] # print "================= extract_diagonal_from_calk_wil_tree =======================" # print_fare_fraction_list(tree_as_breadth_1st_trav_list_upto_level) n = get_num_of_complete_level_for_a_list(tree_as_breadth_1st_trav_list_upto_level) for level in range(1, n + 1): num_of_items_at_level, start_pos_of_level = num_of_item_at_level(level) if(num_of_items_at_level >= diagonal): position_for_diagnoal = start_pos_of_level + diagonal - 1 list_for_diagonal.append(tree_as_breadth_1st_trav_list_upto_level[position_for_diagnoal - 1]) # print "start_pos_of_level + diagonal - 1 ", start_pos_of_level + diagonal - 1 if(display_option): print_fare_fraction_list(list_for_diagonal) return list_for_diagonal
def detect_gbc_pair_in_fare_sequence(n, fare_fraction_list, smallest_largest_pair_only): # print "detect_gbc_pair_in_fare_sequence n %d"%(n) list_len = len(fare_fraction_list) gbc_result_list = [] gbc_result_str_list = [] gbc_a1_n_list=[] b_1_max = 0 # largest denominator in the sequence if go though entire fare_fraction_list try: for i in range(list_len): b_1 = fare_fraction_list[i][1] if(b_1 > b_1_max): b_1_max = b_1 if(b_1 == n): a_1 = fare_fraction_list[i][0] gbc_a1_n_list.append([a_1, b_1]) if a_1 in prime_list: for j in range(i + 1, list_len): b_2 = fare_fraction_list[j][1] if(b_2 == n): a_2 = fare_fraction_list[j][0] if a_2 in prime_list: # print "n_3 ", n, 'a1/b1 = %d/%d'%(a_1, b_1), 'a2/b2 = %d/%d'%(a_2, b_2), if((a_2 + a_1) == n): i_plus_j = i + j j_minus_i = j - i list_len_minus_j = list_len - j result_str = "list_len = %5d, i + j = %5d, i = %5d, j = %5d, j_minus_i = %5d, list_len_minus_j = %5d, %d/%d , %d/%d, %5.2f %5.2f %5.2f "%(list_len, i_plus_j, i, j, j_minus_i, list_len_minus_j, a_1, b_1, a_2, b_2, float(i)/a_1, float(j)/a_2, float(list_len)/n) print result_str gbc_conf =[i, j, a_1, b_1, a_2, b_2] gbc_result_list.append(gbc_conf) gbc_result_str_list.append(result_str) # if only check smallest_largest_pair if(smallest_largest_pair_only): # use exception to break from inner loop raise Exception("catch this") except: pass tmpstr = "n =%3d, denominator_max =%6d, "%(n, b_1_max) # sys.stdout.write(tmpstr) on_screen = False out_str = print_fare_fraction_list(gbc_a1_n_list,on_screen ) # sys.stdout.write("\n") return_str = tmpstr + out_str gbc_conf_len = len(gbc_result_list) tmp_i = 0 tmp_j = 0 tmp_a_1 = 0 tmp_b_1 = 0 tmp_a_2 = 0 tmp_b_2 = 0 # print "gbc_conf_len: ", gbc_conf_len for i in range(gbc_conf_len): tmp_i += gbc_result_list[i][0] tmp_j += gbc_result_list[i][1] tmp_a_1 += gbc_result_list[i][2] tmp_b_1 += gbc_result_list[i][3] tmp_a_2 += gbc_result_list[i][4] tmp_b_2 += gbc_result_list[i][5] # print "tmp i %4d, j %4d, a_1 %4d, b_1 %4d, a_2 %4d, b_2 %4d"%(tmp_i, tmp_j, tmp_a_1, tmp_b_1, tmp_a_2, tmp_b_2) # only show the lst #if(gbc_conf_len >= 1): # print gbc_result_str_list[gbc_conf_len - 1] return return_str