@@ -50,20 +50,19 @@ def is_bipartite_dfs(graph: defaultdict[int, list[int]]) -> bool:
5050 >>> is_bipartite_dfs({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})
5151 Traceback (most recent call last):
5252 ...
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
5754 >>> 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
5958 >>> is_bipartite_dfs({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]})
6059 Traceback (most recent call last):
6160 ...
62- KeyError: 1
61+ TypeError: Nodes must be integers
6362 >>> is_bipartite_dfs({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]})
6463 Traceback (most recent call last):
6564 ...
66- KeyError: 'b'
65+ TypeError: Nodes must be integers
6766 """
6867
6968 def depth_first_search (node : int , color : int ) -> bool :
@@ -85,6 +84,12 @@ def depth_first_search(node: int, color: int) -> bool:
8584 return False
8685 return visited [node ] == color
8786
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" )
8893 visited : defaultdict [int , int ] = defaultdict (lambda : - 1 )
8994 for node in graph :
9095 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:
173178 return True
174179
175180
176- if __name__ == "__main " :
181+ if __name__ == "__main__ " :
177182 import doctest
178183
179184 result = doctest .testmod ()
0 commit comments