def solver(num_files, x, sizes): sizes = sorted(sizes, reverse=True) printd(sizes) return search_size(x, sizes) pass
def build_graph(w, h, table): g_w = nx.DiGraph() g_w.add_node("source") g_w.add_node("target") for i in xrange(h): for j in xrange(w): g_w.add_node("%d_%d" % (i, j)) g_w.add_node("%d_%d_out" % (i, j)) for i in xrange(h): for j in xrange(w): e_name = "%d_%d" % (i, j) e_out_name = get_out_name(i, j) g_w.add_edge(e_name, e_out_name, {'capacity': table[i][j]}) #up if i > 0: out2 = get_out_name(i - 1, j) g_w.add_edge(out2, e_name, {'capacity': 1}) #left if j > 0: out2 = get_out_name(i, j - 1) g_w.add_edge(out2, e_name, {'capacity': 1}) #right if j < w - 1: out2 = get_out_name(i, j + 1) g_w.add_edge(out2, e_name, {'capacity': 1}) #down if i < h - 1: out2 = get_out_name(i + 1, j) g_w.add_edge(out2, e_name, {'capacity': 1}) for j in xrange(w): e_name = "%d_%d" % (0, j) g_w.add_edge("source", e_name, {'capacity': 1}) e_name = get_out_name(h-1, j) g_w.add_edge(e_name, "target", {'capacity': 1}) for e in sorted(g_w.edges()): (u, v) = e data = g_w.get_edge_data(u, v) printd(e, data) return g_w
def solver(a): perms = itertools.permutations(a) min_swaps = None for p in perms: if is_valid_seq(p): printd("EVALUATING", a, p) s = num_swaps(list(a), p) printd("RESULT: ", s, "perm", p) if min_swaps is None or s < min_swaps: #printd("THIS IS LOWER", s, p) min_swaps = s return min_swaps
def search_size(capacity, sizes): hi = len(sizes) lo = int(math.ceil(len(sizes) / 2)) printd("HI", hi, "LO", lo) while hi != lo: mid = (hi + lo) // 2 solved = is_solvable(mid, capacity, sizes) if solved: #printd("Checking ", hi, lo, " MID=", mid, " solved? YES", groups) hi = mid else: printd("Checking ", hi, lo, " MID=", mid, " solved? NO") if lo >= mid: lo += 1 else: lo = mid return lo
def solver(b, m): print "b", b, "m", m all_possibilities = [(i, p) for i in xrange(0, b) for p in possibilities(i, b)] print all_possibilities print b, len(all_possibilities) + 1 for size in xrange(b-1, len(all_possibilities) + 1): #print "SIZE", size for edge_set in itertools.combinations(all_possibilities, size): edge_set = set(edge_set) if contains_b(edge_set, b) and not has_dup(edge_set): printd(edge_set) g = build_graph(b, edge_set) if num_paths(g, b) == m: obj = next(nx.simple_cycles(g), None) if obj == None: return build_res(edge_set, b) return "IMPOSSIBLE"
def solve_recursive(s, k, start, end): printd("s", s, "k", start, end) if (end - start) == k: if is_clean(s, start, end): return 0 elif s[start:end] == list('-' * k): return 1 else: return None if s[start] == '-' or s[end - 1] == '-': options = [] if s[start] == '-': s2 = deepcopy(s) invert(s2, start, k) cost = solve_recursive(s2, k, start + 1, end) if cost is not None: options.append(cost) if s[end - 1] == '-': printd("HERE", start, end) s2 = deepcopy(s) invert(s2, end - k, k) printd("INVERTED", s, s2) cost = solve_recursive(s2, k, start, end - 1) if cost is not None: options.append(cost) if len(options) > 0: return 1 + min(options) else: return None else: return solve_recursive(s, k, start + 1, end)
def solve_rec(pancakes): printd(pancakes) pancakes = sorted(pancakes) max = pancakes[-1] if max > 3: printd("moving pancakes", pancakes) diff_l = 1 diff_r = int(pancakes[-1] / 2) t_times = [] if diff_l <= diff_r: for i in xrange(diff_l, diff_r + 1): printd("testing" ,i) if i < 2: continue pancakes[-1] -= i #move to another with emptier dish if len(pancakes) > 1 and pancakes[0] + i < pancakes[-1]: pancakes[0] += i t = solve_rec(pancakes) + 1 t_times.append(t) pancakes[0] -= i # move to a new one pancakes.append(i) t = solve_rec(pancakes) + 1 t_times.append(t) pancakes = pancakes[:-1] pancakes[-1] += i if len(t_times) > 0: t = min(max, min(t_times)) else: t = max return t else: printd("p", pancakes, "not moved", "max", max) return max