import utils def manhattan_tourist(n, m, s_cost, w_cost): s = list([0] * m for i in range(0, n)) for i in range(1, n): s[i][0] = s[i - 1][0] + s_cost[i - 1][0] for j in range(1, m): s[0][j] = s[0][j - 1] + w_cost[0][j - 1] for i in range(1, n): for j in range(1, m): s[i][j] = max(s[i-1][j] + s_cost[i-1][j],\ s[i][j-1] + w_cost[i][j-1]) return s[n - 1][m - 1] if __name__ == '__main__': n, m, s_cost, w_cost = utils.read_input_data() result = manhattan_tourist(n, m, s_cost, w_cost) utils.write_output_data(result)
import utils def dp_change(money, coins): best_num_coins = [0] * money for m in range(0, money): if m + 1 in coins: best_num_coins[m] = 1 continue best_num_coins[m] = float('inf') for coin in coins: if m + 1 >= coin: a = best_num_coins[m - coin] + 1 best_num_coins[m] = min(best_num_coins[m], a) return best_num_coins[money - 1] if __name__ == '__main__': money, coins = utils.read_input_data() result = dp_change(money, coins) utils.write_output_data(result)
odd_from = node elif from_to[node]["from"] > from_to[node]["to"]: odd_to = node if odd_from not in graph: graph[odd_from] = [] graph[odd_from].append(odd_to) return odd_from, odd_to def euler_path(strings): graph = {} for string in strings: frm = string.split(' -> ')[0] to = string.split(' -> ')[1].split(',') graph[frm] = to frm, to = add_extra_edge_to_graph(graph) cycle = [] while not empty(graph): start = frm if len(cycle) == 0 \ else max(cycle, key=lambda k: len(graph[k])) new_cycle = find_cycle(graph, start) cycle = new_cycle if len(cycle) == 0 else append_cycle( cycle, new_cycle) cycle.pop(0) return cycle if __name__ == '__main__': strings = utils.read_input_data() result = euler_path(strings) utils.write_output_data('->'.join(result))
if v[i - 1] == w[j - 1]: b[i][j] = "↖" elif s[i][j] == s[i - 1][j]: b[i][j] = "↑" else: b[i][j] = "←" substring = [] build_substring(substring, b, v, n - 1, m - 1) return ''.join(substring) def build_substring(sub, b, v, i, j): if i == j == 0: return if b[i][j] == "↖": sub.insert(0, v[i - 1]) build_substring(sub, b, v, i - 1, j - 1) else: if b[i][j] == "↑": build_substring(sub, b, v, i - 1, j) else: build_substring(sub, b, v, i, j - 1) if __name__ == '__main__': v, w = utils.read_input_data() result = lcs(v, w) utils.write_output_data(result)
dna[substrings[j][i]] += 1 score += dna[max(dna, key=dna.get)] return score def bounds_and_branches_motif_search(t, n, DNA): string_size = len(DNA[0]) searchTree = StartIndexesTree(n, string_size - t) best_score = 0 best_subs = [] i = 0 while i > -1: substrings = utils.get_substrings_by_pos(DNA, t, searchTree.current_indexes()) if i < n - 1: optimistic_score = get_score(substrings, i, t) + (n-i)*t if optimistic_score < best_score: i = searchTree.bypass_move(i) else: i = searchTree.next_vertex(i) else: score = get_score(substrings, n, t) if score > best_score: best_score = score best_subs = substrings[:] i = searchTree.next_vertex(i) return best_subs if __name__ == "__main__": t, n, DNA = utils.read_input_data() result = bounds_and_branches_motif_search(t, n, DNA) utils.write_output_data(result)
import utils def string_spectre(k, string): return [''.join(string[i:i + k]) for i in range(0, len(string) - k + 1)] if __name__ == '__main__': k, string = utils.read_input_data() result = string_spectre(k, string) utils.write_output_data(result)
def get_score(substrings, t_size): score = 0 n = len(substrings) for i in range(0, t_size): dna = {'C': 0, 'A': 0, 'G': 0, 'T': 0} for j in range(0, n): dna[substrings[j][i]] += 1 score += dna[max(dna, key=dna.get)] return score def find_motif(t_size, n, strings): best_score = 0 best_substrings = None string_size = len(strings[0]) iterator = NMerIterator(n, string_size - t_size) while iterator.has_next(): pos = iterator.next() substrings = utils.get_substrings_by_pos(strings, t_size, pos) score = get_score(substrings, t_size) if score > best_score: best_score = score best_substrings = substrings return best_substrings if __name__ == '__main__': t_size, n, strings = utils.read_input_data() result = find_motif(t_size, n, strings) utils.write_output_data(result)