def test_if_in_previous_triangle_then_true(self): s = Solver(7) self.assertEqual(True, s.inside_triangle(4, 0, 2)) self.assertEqual(True, s.inside_triangle(4, 0, 4)) self.assertEqual(True, s.inside_triangle(4, 1, 3)) self.assertEqual(True, s.inside_triangle(4, 2, 2)) self.assertEqual(True, s.inside_triangle(4, 3, 1))
def test_handle_union(self): with self.assertRaises(RegExpParseError): Solver._handle_union([], []) with self.assertRaises(RegExpParseError): Solver._handle_union([1], [2, 3]) self.assert_lists(Solver._handle_union, [None, None], [1, 2], [None], [2]) self.assert_lists(Solver._handle_union, [2, None], [4, 2], [2], [4]) self.assert_lists(Solver._handle_union, [None, 2], [3, 3], [2], [3]) self.assert_lists(Solver._handle_union, [5, 2], [6, 3], [5], [6])
def test_handle_concatenation(self): with self.assertRaises(RegExpParseError): Solver._handle_concatenation([], []) with self.assertRaises(RegExpParseError): Solver._handle_concatenation([1], [2, 3]) self.assert_lists(Solver._handle_concatenation, [None, None], [1, 1], [None], [1]) self.assert_lists(Solver._handle_concatenation, [None, 1], [2, 1], [None], [3]) self.assert_lists(Solver._handle_concatenation, [3, 4], [6, 7], [7], [10])
def test_handle_kleene_star(self): with self.assertRaises(RegExpParseError): Solver._handle_union([], []) with self.assertRaises(RegExpParseError): Solver._handle_union([None], []) self.assert_lists(Solver._handle_kleene_star, [None], [2], [0], [2]) self.assert_lists(Solver._handle_kleene_star, [0], [2], [0], [2]) self.assert_lists(Solver._handle_kleene_star, [1], [2], [float('inf')], [float('inf')]) self.assert_lists(Solver._handle_kleene_star, [3], [3], [float('inf')], [float('inf')])
def test_levels_from_file(self): """ Test the levels from files, assert if all levels were solved. """ file_name = open('levels.txt', 'r') count = 0 count_solved = 0 try: for line in file_name.readlines(): main = Solver() main.output = False text = line.split(" ")[0] grid = helper.create_grid_from_text(text, True) matrix = helper.convert_to_matrix(grid, constant.BOARD_SIZE) if main.solve(matrix): count_solved += 1 count += 1 print(main.total_steps) main.total_steps = 0 finally: file_name.close() self.assertEqual(count, count_solved)
def test_when_two_diamonds_index_zeros_probability_is_75(self): s = Solver(14) self.assertEqual(0.75, s.calculate_probability(0, 2))
def test_when_one_diamond_index_zeros_probability_is_50(self): s = Solver(14) self.assertEqual(0.5, s.calculate_probability(0, 1))
def test_is_certain_when_right_column_contains_two_and_index_is_one(self): s = Solver(5) r = s.populate_right_column(s.calculate_height()[1], 4) self.assertEqual(True, s.is_certain(r, 1))
def test_populate_columns_for_height_four_and_six_diamonds_fills_half_right( self): s = Solver(12) r = s.populate_right_column(s.calculate_height()[1], 6) self.assertEqual([True, True, False, False], r)
def test_one_diamond_hits_zero_zero(self): s = Solver(1) self.assertEqual(1.0, s.get_probability(0, 0))
from main import Solver, print_sudoku if __name__ == "__main__": sudoku = [ [5, 3, 0, 0, 7, 0, 0, 0, 0], [6, 0, 0, 1, 9, 5, 0, 0, 0], [0, 9, 8, 0, 0, 0, 0, 6, 0], [8, 0, 0, 0, 6, 0, 0, 0, 3], [4, 0, 0, 8, 0, 3, 0, 0, 1], [7, 0, 0, 0, 2, 0, 0, 0, 6], [0, 6, 0, 0, 0, 0, 2, 8, 0], [0, 0, 0, 4, 1, 9, 0, 0, 5], [0, 0, 0, 0, 8, 0, 0, 7, 9] ] solver = Solver() solver.set_sudoku(sudoku) solver.solveSudoku() print_sudoku(solver.sudoku)
# Building, training, and producing the new features by DCCA model = DeepCCA(layer_sizes1, layer_sizes2, input_shape1, input_shape2, outdim_size, use_all_singular_values, device=device).double() l_cca = None if apply_linear_cca: l_cca = linear_cca() solver = Solver(model, l_cca, outdim_size, epoch_num, batch_size, learning_rate, reg_par, device=device) train1, train2 = data1[0][0], data2[0][0] val1, val2 = data1[1][0], data2[1][0] test1, test2 = data1[2][0], data2[2][0] # val1=None # test1=None solver.fit(train1, train2, val1, val2, test1, test2) # TODO: Save linear_cca model if needed set_size = [ 0, train1.size(0), train1.size(0) + val1.size(0),
def test_edge_height_for_seven_to_fourteen_is_four(self): s = Solver(7) self.assertEqual(4, s.get_max_edge_height()) s = Solver(14) self.assertEqual(4, s.get_max_edge_height())
def test_three_diamonds_hits_zero_zero(self): s = Solver(3) self.assertEqual(1.0, s.get_probability(0, 0))
def test_one_diamond_misses_zero_two(self): s = Solver(1) self.assertEqual(0.0, s.get_probability(0, 2))
def test_calculate_height_for_fifteen_diamonds_is_four(self): s = Solver(15) self.assertEqual(4, s.calculate_height()[0])
def test_calculate_height_for_fourteen_diamonds_is_two(self): s = Solver(14) self.assertEqual(2, s.calculate_height()[0])
def test_calculate_height_for_six_diamonds_is_two(self): s = Solver(6) self.assertEqual(2, s.calculate_height()[0])
def test_calculate_height_for_five_diamonds_is_zero(self): s = Solver(5) self.assertEqual(0, s.calculate_height()[0])
def test_if_not_in_previous_triangle_then_false(self): s = Solver(7) self.assertEqual(False, s.inside_triangle(4, 0, 6)) self.assertEqual(False, s.inside_triangle(4, 1, 5)) self.assertEqual(False, s.inside_triangle(4, 2, 4)) self.assertEqual(False, s.inside_triangle(4, 3, 3))
from flask import Flask, redirect, url_for, render_template, request, session, flash from datetime import timedelta from main import Solver app = Flask(__name__) app.secret_key = "segredo" app.permanent_session_lifetime = timedelta(seconds=5) solver = Solver() solver.set_sudoku_size(9) @app.route("/", methods=["POST", "GET"]) def home(): if request.method == "POST": column = 0 inputed_sudoku = [] for i in solver.idList: inputed_sudoku.append([]) for n in i: inputed_sudoku[solver.idList.index(i)].append(request.form[n]) solver.set_sudoku(inputed_sudoku) session.permanent = True solver.solveSudoku() return redirect(url_for("solved")) else: return render_template("index.html", idList=solver.idList) @app.route("/solved", methods=["POST", "GET"])
def test_has_suffix(self): with self.assertRaises(RegExpParseError): Solver.has_suffix('', 'a', 0) with self.assertRaises(RegExpParseError): Solver.has_suffix('aaaa', 'b', 30) with self.assertRaises(RegExpParseError): Solver.has_suffix('a+b', 'c', 2) with self.assertRaises(RegExpParseError): Solver.has_suffix('.', '1', 3) self.assertFalse(Solver.has_suffix('ab+c.aba.*.bac.+.+*', 'a', 2)) self.assertFalse(Solver.has_suffix('1*', 'a', 2)) self.assertFalse(Solver.has_suffix('1*', 'c', 500)) self.assertFalse(Solver.has_suffix('a*b+', 'b', 2)) self.assertFalse(Solver.has_suffix('ab+c+', 'b', 13)) self.assertFalse(Solver.has_suffix('ab.c.', 'c', 2)) self.assertFalse(Solver.has_suffix('ab+c.', 'b', 1)) self.assertFalse(Solver.has_suffix('ab+c.bc+a.+ca+b.+*', 'b', 3)) self.assertTrue(Solver.has_suffix('acb..bab.c.*.ab.ba.+.+*a.', 'c', 0)) self.assertTrue(Solver.has_suffix('1*', 'a', 0)) self.assertTrue(Solver.has_suffix('a*b+', 'b', 1)) self.assertTrue(Solver.has_suffix('ab+*1.', 'b', 3)) self.assertTrue(Solver.has_suffix('ab+c+', 'b', 1)) self.assertTrue(Solver.has_suffix('ab+c+*', 'b', 100)) self.assertTrue(Solver.has_suffix('ab+c.', 'b', 0)) self.assertTrue(Solver.has_suffix('ab+c.bc+a.+ca+b.+*', 'a', 1))
from main import Solver test_cases = [{ "case": ["#1 @ 1,3: 4x4", "#2 @ 3,1: 4x4", "#3 @ 5,5: 2x2"], "expected_part1": 4, # Answer is sq inches "expected_part2": 3 }] if __name__ == "__main__": for test in test_cases: solver = Solver(12) solver.solve(test['case']) solver.print_grid() assert solver.make_output_part1( ) == test['expected_part1'], "Expected Part 1: {} - Got: {}".format( test['expected_part1'], solver.make_output_part1()) assert solver.make_output_part2( ) == test['expected_part2'], "Expected Part 2: {} - Got: {}".format( test['expected_part2'], solver.make_output_part2())
def test_four_diamonds_hits_one_two_never(self): s = Solver(4) self.assertEqual(0, s.get_probability(0, 2))
def test_edge_height_for_sixteen_to_twentyseven_is_six(self): s = Solver(16) self.assertEqual(6, s.get_max_edge_height()) s = Solver(27) self.assertEqual(6, s.get_max_edge_height())
def test_six_diamonds_hits_zero_two_always(self): s = Solver(6) self.assertEqual(1.0, s.get_probability(0, 2))
def test_number_of_free_diamonds_is_correct(self): s = Solver(3) self.assertEqual(2, s.get_number_free()) s = Solver(5) self.assertEqual(4, s.get_number_free()) s = Solver(7) self.assertEqual(1, s.get_number_free()) s = Solver(14) self.assertEqual(8, s.get_number_free()) s = Solver(16) self.assertEqual(1, s.get_number_free()) s = Solver(27) self.assertEqual(12, s.get_number_free())
def test_populate_columns_for_height_four_and_eight_diamonds_fills_right( self): s = Solver(14) r = s.populate_right_column(s.calculate_height()[1], 8) self.assertEqual([True, True, True, True], r)
"case": [ "[1518-11-01 00:00] Guard #10 begins shift", "[1518-11-01 00:05] falls asleep", "[1518-11-01 00:25] wakes up", "[1518-11-01 00:30] falls asleep", "[1518-11-01 00:55] wakes up", "[1518-11-01 23:58] Guard #99 begins shift", "[1518-11-02 00:40] falls asleep", "[1518-11-02 00:50] wakes up", "[1518-11-03 00:05] Guard #10 begins shift", "[1518-11-03 00:24] falls asleep", "[1518-11-03 00:29] wakes up", "[1518-11-04 00:02] Guard #99 begins shift", "[1518-11-04 00:36] falls asleep", "[1518-11-04 00:46] wakes up", "[1518-11-05 00:03] Guard #99 begins shift", "[1518-11-05 00:45] falls asleep", "[1518-11-05 00:55] wakes up" ], "expected_part1": 240, "expected_part2": 4455 }] if __name__ == "__main__": for test in test_cases: solver = Solver() solver.solve(test['case']) assert solver.make_output_part1( ) == test['expected_part1'], "Expected: {} - Got: {}".format( test['expected_part1'], solver.make_output_part1()) assert solver.make_output_part2( ) == test['expected_part2'], "Expected: {} - Got: {}".format( test['expected_part2'], solver.make_output_part2())
def test_four_diamonds_hits_one_one_half_the_time(self): s = Solver(4) self.assertEqual(0.5, s.get_probability(1, 1))