@@ -50,20 +50,19 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
50
50
>>> is_bipartite_dfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})
51
51
Traceback (most recent call last):
52
52
...
53
- KeyError: 0
54
-
55
- >>> # FIXME: This test should fails with
56
- >>> # TypeError: list indices must be integers or...
53
+ TypeError: Nodes must be integers
57
54
>>> is_bipartite_dfs({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]})
58
- True
55
+ Traceback (most recent call last):
56
+ ...
57
+ TypeError: Nodes must be integers
59
58
>>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
60
59
Traceback (most recent call last):
61
60
...
62
- KeyError: 1
61
+ TypeError: Nodes must be integers
63
62
>>> is_bipartite_dfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]})
64
63
Traceback (most recent call last):
65
64
...
66
- KeyError: 'b'
65
+ TypeError: Nodes must be integers
67
66
"""
68
67
69
68
def depth_first_search (node : int , color : int ) -> bool :
@@ -85,6 +84,12 @@ def depth_first_search(node: int, color: int) -> bool:
85
84
return False
86
85
return visited [node ] == color
87
86
87
+ for node , neighbours in graph .items ():
88
+ if not isinstance (node , int ):
89
+ raise TypeError ("Nodes must be integers" )
90
+ for neighbour in neighbours :
91
+ if not isinstance (neighbour , int ):
92
+ raise TypeError ("Nodes must be integers" )
88
93
visited : defaultdict [int , int ] = defaultdict (lambda : - 1 )
89
94
for node in graph :
90
95
if visited [node ] == - 1 and not depth_first_search (node , 0 ):
@@ -173,7 +178,7 @@ def is_bipartite_bfs(graph: defaultdict[int, list[int]]) -> bool:
173
178
return True
174
179
175
180
176
- if __name__ == "__main " :
181
+ if __name__ == "__main__ " :
177
182
import doctest
178
183
179
184
result = doctest .testmod ()
0 commit comments