Skip to content

Commit c5519cd

Browse files
committed
Add doctest coverage and type hints to Graph.add_pair
1 parent c3d4b9e commit c5519cd

1 file changed

Lines changed: 28 additions & 1 deletion

File tree

graphs/directed_and_undirected_weighted_graph.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -268,7 +268,34 @@ def __init__(self):
268268
# adding vertices and edges
269269
# adding the weight is optional
270270
# handles repetition
271-
def add_pair(self, u, v, w=1):
271+
def add_pair(self, u, v, w=1) -> None:
272+
"""
273+
Adds an edge between u and v with an optional weight w to an undirected graph
274+
275+
>>> g = Graph()
276+
>>> g.add_pair(1,2)
277+
>>> g.graph[1]
278+
[[1, 2]]
279+
>>> g.graph[2]
280+
[[1, 1]]
281+
>>> g.add_pair(1,2) # testing for duplicates
282+
>>> g.graph[1]
283+
[[1, 2]]
284+
>>> g.add_pair(2,1) # reverse order, should not add a duplicate
285+
>>> g.graph[2]
286+
[[1, 1]]
287+
>>> g.add_pair(1,3,5)
288+
>>> g.graph[1]
289+
[[1, 2], [5, 3]]
290+
>>> g.graph[3]
291+
[[5, 1]]
292+
>>> g.add_pair(4,4) # test for self loop
293+
>>> g.graph[4]
294+
[[1, 4]]
295+
>>> g.add_pair(1,2,3) # previously added nodes, different weight
296+
>>> g.graph[1]
297+
[[1, 2], [5, 3], [3, 2]]
298+
"""
272299
# check if the u exists
273300
if self.graph.get(u):
274301
# if there already is a edge

0 commit comments

Comments
 (0)