def indeq(terms): ''' Solve indeterminate equation by extended Euclid's algorithm. terms: Expressed (a,b). This tuple is composed a of ax and b of by. ''' x1 = 1 y1 = 0 z1 = terms[0] x2 = 0 y2 = 1 z2 = terms[1] if coprimep(z1, z2) == False: raise ValueError("Sorry, can only relatively prime number given.") while z2 > 1: r = (z1-(z1%z2))/z2 x1 = x1-(r*x2) y1 = y1-(r*y2) z1 = z1-(r*z2) x1,x2 = ut.swap(x1,x2) y1,y2 = ut.swap(y1,y2) z1,z2 = ut.swap(z1,z2) return (x2,y2)
def greed_fence_repair(cutnum, cut_length_values): ''' Fence repair by greed method ''' cost = 0 while cutnum > 1: first_cut_idx = 0 second_cut_idx = 1 if cut_length_values[first_cut_idx] > cut_length_values[second_cut_idx]: first_cut_idx, second_cut_idx = ut.swap(first_cut_idx, second_cut_idx) i = 2 while True: if i < cutnum: if cut_length_values[i] < cut_length_values[first_cut_idx]: second_cut_idx = first_cut_idx first_cut_idx = i elif cut_length_values[i] < cut_length_values[second_cut_idx]: second_cut_idx = i i += 1 else: break now_cost = cut_length_values[first_cut_idx]+cut_length_values[second_cut_idx] cost += now_cost if first_cut_idx == cutnum-1: first_cut_idx, second_cut_idx = ut.swap(first_cut_idx, second_cut_idx) cut_length_values[first_cut_idx] = now_cost cut_length_values[second_cut_idx] = cut_length_values[cutnum-1] cutnum -= 1 return cost