Beispiel #1
0
class FileSystem:
    def __init__(self, root):
        self.file_system = Tree(root)
        self.level = []

    def add_root_dirs(self):
        for _dir in sorted(os.listdir('.')):
            self.file_system.add_child(self.file_system.root.value,
                                       os.path.abspath(_dir))
        self.level = self.file_system.root.children

    def add_next_level(self):
        while self.level != []:
            for child in self.level:
                if os.path.isdir(child.value):
                    os.chdir(child.value)
                    for c in os.listdir(child.value):
                        self.file_system.add_child(child.value,
                                                   os.path.realpath(c))
                os.chdir('..')
            self.level = [c for c in child.children for child in self.level]

    def flatten_file_system1(self):
        return self.flatten()

    def flatten(self):
        return [
            val for subxs in self.file_system.tree_levels() for val in subxs
        ]

    def flatten_file_system2(self):
        return self.file_system.DFS(self.file_system.root)
Beispiel #2
0
class TestTree(unittest.TestCase):
    def setUp(self):
        self.tree = Tree(5)

    def test_init(self):
        self.assertEqual(self.tree.root.value, 5)

    def test_add_child(self):
        self.assertTrue(self.tree.add_child(5, 4))
        self.assertFalse(self.tree.add_child(6, 3))

    def test_find(self):
        self.tree.add_child(5, 4)
        self.tree.add_child(4, 3)
        self.assertTrue(self.tree.find(4))
        self.assertFalse(self.tree.find(6))

    def test_height(self):
        self.tree.add_child(5, 4)
        self.tree.add_child(4, 3)
        self.assertEqual(self.tree.height(), 2)

    def test_tree_levels(self):
        self.tree.add_child(5, 4)
        self.tree.add_child(4, 3)
        self.assertEqual(self.tree.tree_levels(), [[5], [4], [3]])

    def test_DFS(self):
        self.tree.add_child(5, 4)
        self.tree.add_child(5, 7)
        self.tree.add_child(7, 8)
        self.tree.add_child(4, 3)
        self.tree.add_child(4, 6)
        self.assertEqual(self.tree.DFS(self.tree.root), [5, 7, 8, 4, 6, 3])
Beispiel #3
0
def main():
    inputString = "div[class=main]>h1{hello}+p{it's work!}^div[class=content]{gagaga}"
    operationPattern = "[+*>^{}\[\]]"
    tags = re.split(operationPattern, inputString)
    tags = list(filter(None, tags))
    operations = re.findall(operationPattern, inputString)

    tagId = 0
    parentID = -1
    tree = Tree()
    startTag = Tag(tagId, parentID, "startTag", {}, "")
    tree.appendLeaf(startTag)

    tagId = tagId + 1
    firstTag = Tag(tagId, startTag.leafId, tags.pop(0), {}, "")
    startTag.appendChild(firstTag)
    tree.appendLeaf(firstTag)
    for val in tags:
        tagId = tagId + 1
        operation = operations.pop(0)
        if operation == ">":
            lastAddedTag = tree.getLastAdded()
            tag = Tag(tagId, lastAddedTag.leafId, val, {}, "")
            lastAddedTag.appendChild(tag)
            tree.appendLeaf(tag)
        if operation == "+":
            lastAddedTag = tree.getLastAdded()
            parent = tree.getElementById(lastAddedTag.parentId)
            tag = Tag(tagId, parent.leafId, val, {}, "")
            parent.appendChild(tag)
            tree.appendLeaf(tag)
        if operation == "*":
            lastAddedTag = tree.getLastAdded()
            for i in range(1, int(val)):
                tag = Tag(lastAddedTag.leafId + i, lastAddedTag.parentId,
                          lastAddedTag.tagName, lastAddedTag.attributes,
                          lastAddedTag.textInTag)
                parent = tree.getElementById(lastAddedTag.parentId)
                parent.appendChild(tag)
                tree.appendLeaf(tag)
        if operation == "^":
            lastAddedTag = tree.getLastAdded()
            parent = tree.getElementById(lastAddedTag.parentId)
            grandparent = tree.getElementById(parent.parentId)
            while operations[0] == "^":
                operations.pop(0)
                grandparent = tree.getElementById(grandparent.parentId)
            tag = Tag(tagId, grandparent.leafId, val, {}, "")
            grandparent.appendChild(tag)
            tree.appendLeaf(tag)
        if operation == "{":
            lastAddedTag = tree.getLastAdded()
            lastAddedTag.textInTag = val
            operations.pop(0)
        if operation == "[":
            lastAddedTag = tree.getLastAdded()
            attributes = val.split(',')
            for attribute in attributes:
                key, value = attribute.split("=")
                value = value.replace("\"", '')
                lastAddedTag.attributes[key] = value
            operations.pop(0)
    tree.DFS(0)
    print(startTag.WrapInTag())
Beispiel #4
0
    for size in N:
        for randomize_moves in MOVES_TO_RANDOMIZE:
            # Create board according to size
            board = Board(size)
            # Randomize the board according move counts
            board.randomize(randomize_moves)

            for strategy in STRATEGIES:
                decision_tree = Tree(board)
                # Start count time for solving the problem
                start = time.time()
                # Choose the strategy solution
                if strategy == 'DSF':
                    out = decision_tree.BFS()
                elif strategy == 'BSF':
                    out = decision_tree.DFS()
                elif strategy == 'IDS':
                    out = decision_tree.IDS()
                elif strategy == 'A_STA_A':
                    out = decision_tree.A_star(
                        lambda x: decision_tree.heuristic_a(x))
                elif strategy == 'A_STAR_B':
                    out = decision_tree.A_star(
                        lambda x: decision_tree.heuristic_b(x))
                elif strategy == 'A_STAR_MANHATTAN':
                    out = decision_tree.A_star(
                        lambda x: decision_tree.manhattan_distance(x))
                else:
                    pass
                # Ends the execution time
                end = time.time()