Sha256: e96e5136c6dee978bf4a97c5d31023d7c91988a5c2000b2e691af9405082d6c4

Contents?: true

Size: 1.49 KB

Versions: 125

Compression:

Stored size: 1.49 KB

Contents

class Record():
    def __init__(self, record_id, parent_id):
        self.record_id = record_id
        self.parent_id = parent_id


class Node():
    def __init__(self, node_id):
        self.node_id = node_id
        self.children = []


def BuildTree(records):
    root = None
    records.sort(key=lambda x: x.record_id)
    ordered_id = [i.record_id for i in records]
    if records:
        if ordered_id[-1] != len(ordered_id) - 1:
            raise ValueError
        if ordered_id[0] != 0:
            raise ValueError
    trees = []
    parent = {}
    for i in range(len(ordered_id)):
        for j in records:
            if ordered_id[i] == j.record_id:
                if j.record_id == 0:
                    if j.parent_id != 0:
                        raise ValueError
                if j.record_id < j.parent_id:
                    raise ValueError
                if j.record_id == j.parent_id:
                    if j.record_id != 0:
                        raise ValueError
                trees.append(Node(ordered_id[i]))
    for i in range(len(ordered_id)):
        for j in trees:
            if i == j.node_id:
                parent = j
        for j in records:
            if j.parent_id == i:
                for k in trees:
                    if k.node_id == 0:
                        continue
                    if j.record_id == k.node_id:
                        child = k
                        parent.children.append(child)
    if len(trees) > 0:
        root = trees[0]
    return root

Version data entries

125 entries across 125 versions & 1 rubygems

Version Path
trackler-2.2.1.180 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.179 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.178 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.177 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.176 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.175 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.174 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.173 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.172 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.171 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.170 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.169 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.167 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.166 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.165 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.164 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.163 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.162 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.161 tracks/python/exercises/tree-building/tree_building.py
trackler-2.2.1.160 tracks/python/exercises/tree-building/tree_building.py