def __init__(self, listOfNodeLabels=[], listOfEdges=[]):
        super(Graph, self).__init__()
        if len(listOfNodeLabels) != 0:
            if type(listOfNodeLabels[0]) == str: nodes = [Node(labelstr) for labelstr in listOfNodeLabels if type(labelstr) == str]
            elif type(listOfNodeLabels[0]) == int: nodes = [Node(labelint) for labelint in listOfNodeLabels if type(labelint) == int]
            elif type(listOfNodeLabels[0]) == Node: nodes = [node for node in listOfNodeLabels if type(node) == Node]
            if len(nodes) == len(listOfNodeLabels): self.nodes = nodes
            else: self.nodes = []
        else: self.nodes = []
        self.edges = []

        if len(listOfEdges) != 0:
            edges = []
            if type(listOfEdges[0]) == Edge: edges = [edge for edge in listOfEdges if type(edge) == Edge]
            elif len(listOfEdges[0]) == 2 and type(listOfEdges[0]) == list and type(listOfEdges[0][0]) == str and type(listOfEdges[0][1]) == str:
                edges = []
                for edge in listOfEdges:
                    nodeStart = self.get_node_by_label(edge[0])
                    nodeDest  = self.get_node_by_label(edge[1])
                    if nodeStart != None and nodeDest != None : edges.append(Edge(nodeStart, nodeDest))
                if len(edges) == len(listOfEdges): self.edges = edges
            elif len(listOfEdges[0]) == 3 and type(listOfEdges[0]) == list and type(listOfEdges[0][0]) == str and type(listOfEdges[0][1]) == str and (type(listOfEdges[0][2]) == int or type(listOfEdges[0][2]) == float):
                edges = []
                for edge in listOfEdges:
                    nodeStart = self.get_node_by_label(edge[0])
                    nodeDest  = self.get_node_by_label(edge[1])
                    if nodeStart != None and nodeDest != None : edges.append(Edge(nodeStart, nodeDest, capacity=edge[2]))
                if len(edges) == len(listOfEdges): self.edges = edges
            elif len(listOfEdges[0]) == 3 and type(listOfEdges[0]) == list and type(listOfEdges[0][2]) == int and type(listOfEdges[0][2]) == int and type(listOfEdges[0][2]) == int or type(listOfEdges[0][2]) == float:
                edges = []
                for edge in listOfEdges:
                    nodeStart = self.get_node_by_label(edge[0])
                    nodeDest  = self.get_node_by_label(edge[1])
                    if nodeStart != None and nodeDest != None : edges.append(Edge(nodeStart, nodeDest, capacity=edge[2]))
            elif len(listOfEdges[0]) == 4 and (type(listOfEdges[0]) == list) and (type(listOfEdges[0][0]) == str) and (type(listOfEdges[0][1]) == str) and (type(listOfEdges[0][2]) == int or type(listOfEdges[0][2]) == float) and (type(listOfEdges[0][3]) == int or type(listOfEdges[0][3]) == float):
                edges = []
                for edge in listOfEdges:
                    nodeStart = self.get_node_by_label(edge[0])
                    nodeDest  = self.get_node_by_label(edge[1])
                    if nodeStart != None and nodeDest != None : edges.append(Edge(nodeStart, nodeDest, capacity=edge[2], flow=edge[3]))
            elif len(listOfEdges[0]) == 4 and type(listOfEdges[0]) == list and (type(listOfEdges[0][2]) == int) and (type(listOfEdges[0][2]) == int) and (type(listOfEdges[0][2]) == int or type(listOfEdges[0][2]) == float) and (type(listOfEdges[0][3]) == int or type(listOfEdges[0][3]) == float):
                edges = []
                for edge in listOfEdges:
                    nodeStart = self.get_node_by_label(edge[0])
                    nodeDest  = self.get_node_by_label(edge[1])
                    if nodeStart != None and nodeDest != None : edges.append(Edge(nodeStart, nodeDest, capacity=edge[2], flow=edge[3]))

        self.nodestable = {}
        for node in self.nodes :
            self.nodestable[node.get_label()] = []
Exemple #2
0
def get_graph_1():
    graph = Graph()
    node_a = Node(1)
    node_b = Node(2)
    node_c = Node(3)
    node_d = Node(4)
    node_e = Node(5)

    graph.set_bidirectional_neighbor(node_a, node_b, 40)
    graph.set_bidirectional_neighbor(node_b, node_e, 20)

    graph.set_bidirectional_neighbor(node_a, node_c, 30)
    graph.set_bidirectional_neighbor(node_c, node_d, 10)
    graph.set_bidirectional_neighbor(node_d, node_e, 30)

    graph.add_nodes([node_a, node_b, node_c, node_d, node_e])

    return graph, node_a, node_e
Exemple #3
0
def solution(L1, L2):
    n = L = None
    prev = None
    val = 0
    carry = 0
    for n1, n2 in zip(L1, L2):
        sum = n1.d + n2.d + carry
        val = sum % 10
        carry = sum // 10
        new_node = Node(val)
        if L is None:
            L = new_node
        else:
            prev.next = new_node
        prev = new_node

    if carry != 0:
        prev.next = Node(carry)

    return L
 def add_node (self, nodeOrLabel):
     if type(nodeOrLabel) == Node:
         self.nodes.append(nodeOrLabel)
     elif type(nodeOrLabel) == str:
         self.nodes.append(Node(nodeOrLabel))
Exemple #5
0
def solution(L):
    prev = None
    for n in L:
        if n.next is None:
            break
        else:
            prev = n

    if prev is None:
        return L.d
    else:
        return prev.d


L0 = Node(1)

L1 = Node(1)
[L1.append(x) for x in [1]]

L2 = Node(1)
[L2.append(x) for x in [2]]

L3 = Node(2)
[L3.append(x) for x in [1, 1, 2, 3, 5, 4]]


@pytest.mark.parametrize("L, expected", [
    (L0, 1),
    (L1, 1),
    (L2, 1),
                    except Exception as e:
                        print('EXCEPTION', first, n, prev)
                        raise e
                else:
                    print('!=', first.d, n.d)
                    prev = n
                n = n.next

            first = first.next

    while remove_dup(L):
        pass
    return L


L0 = Node(1)

L1 = Node(1)
[L1.append(x) for x in [1]]
L1E = Node(1)

L2 = Node(1)
[L2.append(x) for x in [2]]

L3 = Node(1)
[L3.append(x) for x in [2, 1, 2, 3, 1, 4]]

L3E = Node(1)
[L3E.append(x) for x in [2, 3, 4]]