def test(): changes = 11 print('coin_change_iter_dup') for x in coin_change_iter_dup(changes): print(x) #coin_change_iter_dup(changes) show_call_counter() print('coin_change_memo') print(coin_change_memo(changes)) show_call_counter() print('coin_change_iter') for x in coin_change_iter(COINS[:], changes): print(x) show_call_counter() print('coin_change_iter2') for x in coin_change_iter2(COINS[:], changes): print(x) show_call_counter() print('change') for x in change(changes, COINS[:], []): print(x) show_call_counter()
def test(): A = [1, 2, 3, 4] print('standard:') print([c for c in itertools.combinations(A, 2)]) print('combinations_iter') print([c for c in combinations_iter(A, 2)]) show_call_counter()
def courses_noloop(prerequisites:List[List]): cd = defaultdict(list) for c, d in prerequisites: cd[c].append(d) cd[d].extend([]) for c in cd: if detect_loop(cd, c): return False show_call_counter() return True
def test(): cities = { 'a': [('b', 10), ('c', 15), ('d', 20)], 'b': [('c', 35), ('d', 25)], 'c': [('d', 30)], } cg = CityGraph(cities) cg.show() print(tsp(cg, 'a')) show_call_counter()
def test(): #data = [0,1,0,1,0,0,0,0,0] data = [0, 1, 0, 1, 1, 1, 0, 0, 0] min_skip = 0 max_skip = 3 print('walk_skip:') for p in walk_skip(data, min_skip, max_skip): print(p) show_call_counter() print('walk_skip2:') walk_skip2(data, min_skip, max_skip) show_call_counter()
def test_fixture(solution): testdata = [ # (input, expect), (([1]), [[1]]), (([1, 1, 2]), [[1, 1, 2], [1, 2, 1], [2, 1, 1]]), (([1, 2, 3]), [[1, 2, 3], [1, 3, 2], [2, 1, 3], [2, 3, 1], [3, 1, 2], [3, 2, 1]]), (([1, 1, 1]), [1, 1, 1]), ] for i in range(len(testdata)): ret = solution.permuteUnique(testdata[i][0]) show_call_counter() exp = sorted(testdata[i][1]) print("{} -> \t{} \t\t{} expect {}".format( testdata[i][0], ret, 'pass' if sorted(ret) == exp else 'fail', exp))
def test(): m = 3 n = 3 print(unique_path_count_recursive(m, n)) show_call_counter() print(unique_path_count_recursive_dp(m, n)) show_call_counter() print(unique_paths_recursive(m, n)) show_call_counter() #print(unique_paths_recursive_memo(m,n)) print(unique_paths_dp(m, n)) show_call_counter()
def test(): V = [60, 100, 120, 40, 30] W = [10, 20, 30, 5, 3] C = 50 print("knapsack_count") print(knapsack_count(V, W, C)) show_call_counter() print("knapsack_recursive") print(knapsack_recursive(V, W, C)) show_call_counter() print("knapsack_bitmap") print(knapsack_bitmap(V, W, C)) show_call_counter()
def test(): print("find_ways") n = 4 c, paths = find_ways(n) print('Total possibilities for {} stairs: {} '.format(n, c)) for path in paths: print(path) n = 5 print('Total possibilities for {} stairs: {} '.format(n, count_ways(n))) print('Count ways memo for {} stairs: {} '.format(n, count_ways_memo(n))) # robot_in_grid grid = [ [1, 1, 1, 1, 0, 1], [1, 0, 1, 0, 1, 1], [1, 1, 1, 1, 1, 0], [1, 0, 1, 0, 1, 1], ] print('robot_in_grid:') for path in robot_in_grid(grid, 0, 0): print(path) print('robot_in_grid_memo:') paths_memo = robot_in_grid_memo(grid, 0, 0) if (0, 0) in paths_memo: for path in paths_memo[(0, 0)]: print(path) # magic_index print('magic_index') a = [-40, -20, -1, 1, 2, 3, 5, 7, 9, 12, 13] print('{} -> {}'.format(a, magic_index(a))) # 7 a = [-40, -20, -1, 1, 2, 3, 5, 6, 7, 8, 12, 13] print('{} -> {}'.format(a, magic_index(a))) # None # all_subsets print('all_subsets') s = all_subsets({1, 2, 3, 4}) print(s, 'count:', len(s)) s = all_subsets_bitmap({1, 2, 3, 4}) print(s, 'count:', len(s)) # recursive_multiply print('recursive_multiply') print("{}*{}={}".format(3, 4, recursive_multiply(3, 4))) print("{}*{}={}".format(0, 5, recursive_multiply(0, 5))) print("{}*{}={}".format(2, 15, recursive_multiply(2, 15))) print("{}*{}={}".format(4, 0, recursive_multiply_bitshift(4, 0))) print("{}*{}={}".format(3, 15, recursive_multiply_bitshift(3, 15))) print("{}*{}={}".format(30, 5, recursive_multiply_bitshift(30, 5))) # towers_of_hanoi s1 = [10, 9, 8, 7, 6, 5, 4, 3, 2, 1] s1, s2, s3 = towers_of_hanoi(s1) print('s1:', s1) print('s2:', s2) print('s3:', s3) # str_permutations_withsub s = 'abc' print('str_permutations_withsub: {}'.format(s)) p = str_permutations_withsub(s) print(p) print('total: ', len(p)) print('str_permutations: {}'.format(s)) p = str_permutations(s) print(p) print('total: ', len(p)) print('str_permutations_withdup') print(str_permutation_withdup('abb')) c = Counter('abb') print('str_permutations_withdup_op') print(str_permutation_withdup_op(c)) # valid_parentheses print('valid_parentheses') n = 3 d = {'(': n, ')': n} print(valid_parentheses(d)) # paint_fill image = [ [2, 2, 1, 1, 2, 1, 5, 4, 1, 1], [1, 2, 2, 1, 1, 1, 1, 4, 4, 2], [1, 2, 5, 5, 1, 4, 4, 4, 2, 2], [5, 1, 1, 4, 1, 4, 4, 1, 2, 2], [4, 5, 5, 1, 1, 1, 1, 1, 1, 1], ] color = 0 x = 4 y = 1 for line in image: print(line) print("paint_fill to {} at ({},{})".format(color, x, y)) paint_fill(image, x, y, color) for line in image: print(line) # coins n = 34 print('coins_fullinfo for ', n) print(coins_fullinfo(n)) print('coins_ways_num for ', n) print(coins_ways_num(n)) print('coins_ways_num_memo for ', n) print(coins_ways_num_memo(n)) print(show_call_counter()) # eight_queens print('eight_queens') #output, total = eight_queens() #print('tried:', total) #output = eight_queens_memo() # run long time > 10min # print(show_call_counter()) # print(output) # for solution in output: # print('_'*30) # for y in range(8): # for x in range(8): # if (x,y) in solution: # print('1 ',end='') # else: # print('0 ', end='') # print('') # print('available solutions:', len(output)) output = eight_queens2() for rows in output: print("=" * 16) for r in range(8): col = rows[r] print('- ' * (col) + 'x ' + '- ' * (7 - col)) print(show_call_counter()) print('solutions: {}'.format(len(output))) # count_evals print('count_evals') s = '1^0|0|1' d = False print('{},{} -> {}'.format(s, d, count_evals(s, d))) s = '0&0&0&1^1|0' d = True print('{},{} -> {}'.format(s, d, count_evals(s, d))) print(show_call_counter()) print('count_evals_memo') s = '1^0|0|1' d = False print('{},{} -> {}'.format(s, d, count_evals_memo(s, d))) s = '0&0&0&1^1|0' d = True print('{},{} -> {}'.format(s, d, count_evals_memo(s, d))) print(show_call_counter())