class FinderTests(unittest.TestCase): def setUp(self): self.g = nx.DiGraph() self.f = Finder(self.g) def testFindsAPath(self): self.g.add_nodes_from([0, 1, 2, 3, 4]) self.g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4)]) # Can't go from 1 to 3, or 2 to 1 test = sorted(self.f.find_paths_from(0)) self.assertEqual(test, [[0, 1, 3], [0, 1, 4], [0, 2]]) def testHandlesCyclicGraphs(self): self.g.add_nodes_from([0, 1, 2]) self.g.add_edges_from([(0, 1), (1, 2), (2, 0)]) test = sorted(self.f.find_paths_from(0)) self.assertEqual(sorted(self.f.find_paths_from(0)), [[0, 1, 2]]) def testFindsAllPaths(self): self.g.add_nodes_from([0, 1, 2, 3]) self.g.add_edges_from([(0, 1), (1, 0), (0, 2), (2, 3), (3, 1), (1, 2)]) self.assertEqual(sorted(self.f.find_all_paths()), sorted([ [0, 1, 2, 3], [0, 2, 3, 1], [1, 0, 2, 3], [1, 2, 3], [2, 3, 1, 0], [3, 1, 2], [3, 1, 0, 2]]))
class FinderTests(unittest.TestCase): def setUp(self): self.g = nx.DiGraph() self.f = Finder(self.g) def testFindsAPath(self): self.g.add_nodes_from([0, 1, 2, 3, 4]) self.g.add_edges_from([(0, 1), (0, 2), (1, 3), (1, 4)]) # Can't go from 1 to 3, or 2 to 1 test = sorted(self.f.find_paths_from(0)) self.assertEqual(test, [[0, 1, 3], [0, 1, 4], [0, 2]]) def testHandlesCyclicGraphs(self): self.g.add_nodes_from([0, 1, 2]) self.g.add_edges_from([(0, 1), (1, 2), (2, 0)]) test = sorted(self.f.find_paths_from(0)) self.assertEqual(sorted(self.f.find_paths_from(0)), [[0, 1, 2]]) def testFindsAllPaths(self): self.g.add_nodes_from([0, 1, 2, 3]) self.g.add_edges_from([(0, 1), (1, 0), (0, 2), (2, 3), (3, 1), (1, 2)]) self.assertEqual( sorted(self.f.find_all_paths()), sorted([[0, 1, 2, 3], [0, 2, 3, 1], [1, 0, 2, 3], [1, 2, 3], [2, 3, 1, 0], [3, 1, 2], [3, 1, 0, 2]]))
def scores_for(puzzle): puzzle = normalize_nums(puzzle) F = Finder(Grid(puzzle)) return sorted([ (x, F.graph.path_values(y), y) for x, y in [(Score.calculate_from(F.graph.path_values(path)), path) for path in F.find_all_paths()] ])
def scores_for(puzzle): puzzle = normalize_nums(puzzle) F = Finder(Grid(puzzle)) return sorted([(x, F.graph.path_values(y), y) for x, y in [ (Score.calculate_from(F.graph.path_values(path)), path) for path in F.find_all_paths() ] ])