-
Notifications
You must be signed in to change notification settings - Fork 891
/
problem_218.py
71 lines (52 loc) · 1.44 KB
/
problem_218.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
class Node:
def __init__(self, iden):
self.iden = iden
def __hash__(self):
return hash(self.iden)
def __eq__(self, other):
return self.iden == other.iden
def __repr__(self):
return str(self.iden)
class Edge:
def __init__(self, src, tgt):
self.src = src
self.tgt = tgt
def __hash__(self):
return hash((self.src, self.tgt))
def __eq__(self, other):
return self.src == other.src and self.tgt == other.tgt
def __repr__(self):
return "{}->{}".format(self.src, self.tgt)
def reverse(self):
tmp_node = self.src
self.src = self.tgt
self.tgt = tmp_node
class Graph:
def __init__(self):
self.nodes = set()
self.edges = set()
def add_node(self, node):
if node in self.nodes:
return
self.nodes.add(node)
def add_edge(self, src_node, tgt_node):
self.edges.add(Edge(src_node, tgt_node))
def reverse_edges(self):
self.edges = [Edge(x.tgt, x.src) for x in self.edges]
def get_edges(self):
return self.edges
# Tests
g = Graph()
a = Node('a')
b = Node('b')
c = Node('c')
g.add_node(a)
g.add_node(b)
g.add_node(c)
g.add_edge(a, b)
g.add_edge(b, c)
edges = g.get_edges()
assert Edge(a, b) in edges and Edge(b, c) in edges and len(edges) == 2
g.reverse_edges()
edges = g.get_edges()
assert Edge(b, a) in edges and Edge(c, b) in edges and len(edges) == 2