Ejemplo n.º 1
0
class TestTree(TestCase):
    def setUp(self):
        self.root = Step(None, (0, 0), "O")
        self.leaf1 = self.root.add_leaf(1, ".")
        self.leaf11 = self.leaf1.add_leaf(1, ".")
        self.leaf113 = self.leaf11.add_leaf(3, ".")
        self.leaf1132 = self.leaf113.add_leaf(2, ".")

    def test_terminal(self):
        self.assertSequenceEqual([self.leaf1132],
                                 self.root.get_terminal_leafs())

    def test_loop(self):
        self.leaf11324 = self.leaf1132.add_leaf(4, 'X')
        self.assertIsNone(self.leaf11324)

    def test_traverse(self):
        for node in self.leaf1132.go_upstream():
            print(node.coords, node.depth, node.value)

    def test_search(self):
        self.assertIsNone(self.root.search_by_value('X'))
        self.assertSequenceEqual(self.root.search_by_value("O"), [self.root])
        self.assertSetEqual(
            set(self.root.search_by_value(".")),
            {self.leaf1, self.leaf11, self.leaf113, self.leaf1132})

    def test_print(self):
        self.assertEqual(str(self.leaf11), "(0, -2) - .")
Ejemplo n.º 2
0
                withdraw(direction)
            return


do_step(step_tree, 1)
do_step(step_tree, 2)
do_step(step_tree, 3)
do_step(step_tree, 4)

# At this point we have an unidirectional graph so several leafs may represent the same coordinates.
# For ex 2 it is convenient to reduce leafs to coordinates, which more-less will work as a map
# instead of set of paths.

adjacency_map = dict()
points = []
for terminal_leaf in step_tree.get_terminal_leafs():
    for leaf in terminal_leaf.go_upstream():
        if leaf.value != 0:
            siblings = adjacency_map.get(leaf.coords, set())
            for l in leaf.leafs:
                siblings.add(l.coords)
            if leaf.upstream:
                siblings.add(leaf.upstream.coords)
            adjacency_map[leaf.coords] = siblings

current_points = set([oxygen_coords])
count = 0

# Just traverse the map, but do not go twice over same point
while True:
    next_current_points = set()