def count_pal_substrings(string): pal = [[False for i in string] for j in string] dp = [[0 for i in string] for j in string] for i in range(len(string)): pal[i][i] = True for i in range(1, len(string)): if string[i - 1] == string[i]: pal[i - 1][i] = True dp[i - 1][i] = 1 for l in range(2, len(string)): for i in range(len(string)): j = i + l if j < len(string): if string[i] == string[j] and pal[i + 1][j - 1]: pal[i][j] = True if pal[i][j]: dp[i][j] = 1 + dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][ j - 1] # add sub pals and remove common else: dp[i][j] = dp[i + 1][j] + dp[i][j - 1] - dp[i + 1][j - 1] print_matrix(dp) return dp[0][len(string) - 1]
def minimise_badness(words, width): badness = compute_badness(words, width) cost = [maxint for i in words] + [0] dp = [0 for i in words] i = len(words) - 1 while i >= 0: j = len(words) cost[i] = badness[i][j - 1] dp[i] = len(words) while j > i: if badness[i][j - 1] == maxint: j -= 1 continue if cost[i] > cost[j] + badness[i][j - 1]: cost[i] = cost[j] + badness[i][j - 1] dp[i] = j j -= 1 i -= 1 print_matrix(badness) print dp print cost
def solve(matrix): visited = [[0 for i in matrix] for j in matrix] visited[0][0] = 1 ans = is_rat_reached_destination(matrix, 0, 0, len(matrix), visited) if ans: print_matrix(visited) return ans
def solve(): solution = [[-1 for i in range(8)] for j in range(8)] x_move = [2, 1, -1, -2, -2, -1, 1, 2] y_move = [1, 2, 2, 1, -1, -2, -2, -1] solution[0][0] = 0 if not solve_recur(0, 0, 1, x_move, y_move, solution): print 'Nope' return else: print_matrix(solution)
def is_subset_sum_possible(array, sm): dp = [[False for i in range(len(array) + 1)] for j in range(sm + 1)] for i in range(len(array) + 1): dp[0][i] = True for i in range(1, sm + 1): for j in range(1, len(array) + 1): if array[j - 1] > i: dp[i][j] = dp[i][j - 1] # without including nth element else: dp[i][j] = dp[i][j - 1] or dp[i - array[j - 1]][j - 1] # include and not include print_matrix(dp) return dp[sm][len(array)]
def num_ways_to_parenthesize(string): operands = [string[i] for i in range(len(string)) if i % 2 == 0] operators = [string[i] for i in range(len(string)) if i % 2 != 0] dp = [[1 for i in operands] for j in operands] for l in range(2, len(operands)): for i in range(len(operands)): j = i + l if j < len(operands): dp[i][j] = sum([dp[i][k] * dp[k + 1][j] for k in range(i, j)]) print_matrix(dp) return dp[0][len(operands) - 1]
def get_num_ways_to_make_k_using_1_to_k(n, k): dp = [[0 for i in range(k + 1)] for j in range(n + 1)] for i in range(k + 1): dp[0][i] = 1 for i in range(1, n + 1): for j in range(1, k + 1): dp[i][j] = dp[i][j - 1] # not using j if j <= i: dp[i][j] += dp[i - j][j - 1] # using j print_matrix(dp) return dp[n][k]
def get_number_of_ways(x, y): if x == 0 or y == 0: return 1 matrix = [[0 for i in range(y + 2)] for j in range(x + 2)] for i in range(1, y + 2): matrix[1][i] = 1 for j in range(1, x + 2): matrix[j][1] = 1 for i in range(2, x + 2): for j in range(2, y + 2): matrix[i][j] = matrix[i - 1][j] + matrix[i][j - 1] print_matrix(matrix) return matrix[x + 1][y + 1]
def count_n_digit_nums_of_sum(n, s): dp = [[0 for i in range(s + 1)] for j in range(n + 1)] for i in range(s + 1): dp[0][i] = 0 for i in range(n + 1): dp[i][0] = 1 for num in range(1, n + 1): for _sum in range(1, s + 1): for last_digit in range(10): if last_digit > _sum: break dp[num][_sum] += dp[num - 1][_sum - last_digit] print_matrix(dp) return dp[n][s]
def max_price(prices, n, k): dp = [[0 for i in range(n)] for j in range(k + 1)] # profit on 0th day = 0. Also 0 transactions is 0. # hence first row and column is 0s for i in range(1, k + 1): # max i transactions for j in range(1, n): # I am selling on jth day max_so_far = -maxint for m in range(j): max_so_far = max(max_so_far, prices[j] - prices[m] + dp[i - 1][m]) dp[i][j] = max( dp[i][j - 1], # not doing anything on jth day max_so_far # max by doing on jth day ) print_matrix(dp) return dp[k][n - 1]
def triangulation_cost(polygons): if len(polygons) <= 3: return 0 dp = [[0 for i in polygons] for j in polygons] for l in range(len(polygons)): for i in range(len(polygons)): j = i + l if j < len(polygons): if j >= 2 + i: dp[i][j] = maxint for k in range(i + 1, j): curr_cost = dp[i][k] + dp[k][j] + cost( i, j, k, polygons) print i, j, k, curr_cost if curr_cost < dp[i][j]: dp[i][j] = curr_cost print_matrix(dp)
def max_pal(string): n = len(string) dp = [[0 for i in range(n)] for j in range(n)] for i in range(n): dp[i][i] = 1 j = i + 1 if j < n: dp[i][j] = 2 if string[i] == string[j] else 1 for l in range(2, n): for i in range(n): j = i + l if j < n: dp[i][j] = max(dp[i][j - 1], dp[i + 1][j - 1]) if is_pal(string, i, j): dp[i][j] += 1 print_matrix(dp) return dp[0][n - 1]
if matrix[i][j] == 'G': q.put((i, j, 0)) output_matrix[i][j] = 0 while not q.empty(): el = q.get() dist = el[2] i = el[0] j = el[1] for neighbour in get_neighbours(i, j, m, n, matrix): x = neighbour[0] y = neighbour[1] if output_matrix[x][y] != -1: continue q.put((x, y, dist + 1)) output_matrix[x][y] = dist + 1 return output_matrix if __name__ == '__main__': k = [['O', 'O', 'O', 'O', 'G'], ['O', 'W', 'W', 'O', 'O'], ['O', 'O', 'O', 'W', 'O'], ['G', 'W', 'W', 'W', 'O'], ['O', 'O', 'O', 'O', 'G']] output = get_shortest_dists(k, 5, 5) print_matrix(output)
if loc is None: return True r = loc[0] c = loc[1] for num in range(1, 10): if is_safe(board, r, c, num): board[r][c] = num if solve(board): return True board[r][c] = 0 return False if __name__ == '__main__': grid = [[3, 0, 6, 5, 0, 8, 4, 0, 0], [5, 2, 0, 0, 0, 0, 0, 0, 0], [0, 8, 7, 0, 0, 0, 0, 3, 1], [0, 0, 3, 0, 1, 0, 0, 8, 0], [9, 0, 0, 8, 6, 3, 0, 0, 5], [0, 5, 0, 0, 9, 0, 6, 0, 0], [1, 3, 0, 0, 0, 0, 2, 5, 0], [0, 0, 0, 0, 0, 0, 0, 7, 4], [0, 0, 5, 2, 0, 6, 3, 0, 0]] if solve(grid): print_matrix(grid) else: print 'Nope'
matrix[n - x - 1][n - y - 1] = matrix[y][n - x - 1] matrix[y][n - x - 1] = temp y += 1 x += 1 return matrix if __name__ == '__main__': m = [['a', 'b', 'c', 'd'], ['e', 'f', 'g', 'h'], ['i', 'j', 'k', 'l'], ['m', 'n', 'o', 'p']] print_matrix(m) print '' print_matrix(rotate(m)) print '' m = [['a', 'b', 'c', 'd', 'q'], ['e', 'f', 'g', 'h', 'r'], ['i', 'j', 'k', 'l', 's'], ['m', 'n', 'o', 'p', 't'], ['u', 'v', 'x', 'y', 'z']] print_matrix(m) print ''
matrix[i][0] = 1 for i in range(1, len(matrix)): for j in range(1, len(matrix[0])): if matrix[i][0] == 1 or matrix[0][j] == 1: matrix[i][j] = 1 if r_flag: for i in range(len(matrix[0])): matrix[0][i] = 1 if c_flag: for i in range(len(matrix)): matrix[i][0] = 1 return matrix if __name__ == '__main__': m = [[1, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0]] set_matrix(m) print_matrix(m) print '' m = [[1, 0, 0, 1], [0, 0, 1, 0], [0, 0, 0, 0]] set_matrix_constant_extra_space(m) print_matrix(m)