From 0cf19a8f4102aef0987a57467ce6af8dbe702ac7 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 00:59:57 -0700 Subject: [PATCH 1/8] [ADD] tests for check_bipartite_graph_bfs --- graphs/check_bipartite_graph_bfs.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 7fc57cbc78bd..1a7c7b1a539e 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -10,6 +10,24 @@ def check_bipartite(graph): + """ + >>> check_bipartite( + ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} + ... ) + True + >>> check_bipartite( + ... {0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]} + ... ) + False + >>> check_bipartite( + ... {0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]} + ... ) + True + >>> check_bipartite( + ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} + ... ) + False + """ queue = Queue() visited = [False] * len(graph) color = [-1] * len(graph) From cd41bc9f2c13597319970b7735d1bc0bda388269 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 01:08:06 -0700 Subject: [PATCH 2/8] linter fix? --- graphs/check_bipartite_graph_bfs.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 1a7c7b1a539e..1320f78f879d 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -12,8 +12,8 @@ def check_bipartite(graph): """ >>> check_bipartite( - ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} - ... ) + ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} + ... ) True >>> check_bipartite( ... {0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]} From 1a7ada6779903e327015ab3132e17446ba68a573 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 01:11:32 -0700 Subject: [PATCH 3/8] linter fix --- graphs/check_bipartite_graph_bfs.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 1320f78f879d..85b658b14f1e 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -16,16 +16,16 @@ def check_bipartite(graph): ... ) True >>> check_bipartite( - ... {0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]} - ... ) + ... {0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]} + ... ) False >>> check_bipartite( - ... {0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]} - ... ) + ... {0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]} + ... ) True >>> check_bipartite( - ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} - ... ) + ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} + ... ) False """ queue = Queue() From 2db5074ac0febab9bf5626cac73cdf7c3385c79c Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 14:10:14 -0700 Subject: [PATCH 4/8] [ADD] more test cases check_bipartite_graph_bfs --- graphs/check_bipartite_graph_bfs.py | 34 +++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 85b658b14f1e..26c7bdc7cc32 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -11,6 +11,8 @@ def check_bipartite(graph): """ + >>> check_bipartite({}) + True >>> check_bipartite( ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} ... ) @@ -27,6 +29,38 @@ def check_bipartite(graph): ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} ... ) False + >>> check_bipartite( + ... {7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} + ... ) + KeyError: 0 + >>> check_bipartite( + ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]} + ... ) + KeyError: 4 + >>> check_bipartite( + ... {0: [-1, 3], 1: [0, -2]} + ... ) + KeyError: -1 + >>> check_bipartite( + ... {-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]} + ... ) + True + >>> check_bipartite( + ... {0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} + ... ) + KeyError: 0 + >>> check_bipartite( + ... {0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]} + ... ) + TypeError: list indices must be integers or slices, not float + >>> check_bipartite( + ... {"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]} + ... ) + KeyError: 0 + >>> check_bipartite( + ... {0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]} + ... ) + TypeError: list indices must be integers or slices, not str """ queue = Queue() visited = [False] * len(graph) From b65b2f53f03092e7ad14e8326103d467646e3823 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 14:23:41 -0700 Subject: [PATCH 5/8] doctest fixes. Forgot to add 'Traceback...' --- graphs/check_bipartite_graph_bfs.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 26c7bdc7cc32..666978261063 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -32,15 +32,21 @@ def check_bipartite(graph): >>> check_bipartite( ... {7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} ... ) + Traceback (most recent call last): + ... KeyError: 0 >>> check_bipartite( ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]} ... ) + Traceback (most recent call last): + ... KeyError: 4 >>> check_bipartite( ... {0: [-1, 3], 1: [0, -2]} ... ) - KeyError: -1 + Traceback (most recent call last): + ... + IndexError: list index out of range >>> check_bipartite( ... {-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]} ... ) @@ -48,6 +54,8 @@ def check_bipartite(graph): >>> check_bipartite( ... {0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} ... ) + Traceback (most recent call last): + ... KeyError: 0 >>> check_bipartite( ... {0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]} @@ -56,10 +64,14 @@ def check_bipartite(graph): >>> check_bipartite( ... {"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]} ... ) + Traceback (most recent call last): + ... KeyError: 0 >>> check_bipartite( ... {0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]} ... ) + Traceback (most recent call last): + ... TypeError: list indices must be integers or slices, not str """ queue = Queue() @@ -97,3 +109,5 @@ def bfs(): if __name__ == "__main__": # Adjacency List of graph print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})) + import doctest + doctest.testmod() \ No newline at end of file From b5aed598a107b2c0db6b6198e24dc31de83e3a29 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 19 Oct 2023 21:24:33 +0000 Subject: [PATCH 6/8] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- graphs/check_bipartite_graph_bfs.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 666978261063..47a0d87b8829 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -110,4 +110,5 @@ def bfs(): # Adjacency List of graph print(check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]})) import doctest - doctest.testmod() \ No newline at end of file + + doctest.testmod() From d7f52200c68ba1130fa2545a3603a85405370724 Mon Sep 17 00:00:00 2001 From: RaymondDashWu Date: Thu, 19 Oct 2023 20:21:07 -0700 Subject: [PATCH 7/8] missed a Traceback --- graphs/check_bipartite_graph_bfs.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 666978261063..59c7df6979c0 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -60,6 +60,8 @@ def check_bipartite(graph): >>> check_bipartite( ... {0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]} ... ) + Traceback (most recent call last): + ... TypeError: list indices must be integers or slices, not float >>> check_bipartite( ... {"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]} From 1520e1a5c303016813d6e11d09ca42c8d9d44022 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Fri, 20 Oct 2023 06:38:56 +0200 Subject: [PATCH 8/8] Update check_bipartite_graph_bfs.py --- graphs/check_bipartite_graph_bfs.py | 48 ++++++++--------------------- 1 file changed, 12 insertions(+), 36 deletions(-) diff --git a/graphs/check_bipartite_graph_bfs.py b/graphs/check_bipartite_graph_bfs.py index 77a59d747e74..6c385d54e0b6 100644 --- a/graphs/check_bipartite_graph_bfs.py +++ b/graphs/check_bipartite_graph_bfs.py @@ -13,65 +13,41 @@ def check_bipartite(graph): """ >>> check_bipartite({}) True - >>> check_bipartite( - ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} - ... ) + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) True - >>> check_bipartite( - ... {0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]} - ... ) + >>> check_bipartite({0: [1, 2, 3], 1: [0, 2], 2: [0, 1, 3], 3: [0, 2]}) False - >>> check_bipartite( - ... {0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]} - ... ) + >>> check_bipartite({0: [4], 1: [], 2: [4], 3: [4], 4: [0, 2, 3]}) True - >>> check_bipartite( - ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} - ... ) + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) False - >>> check_bipartite( - ... {7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]} - ... ) + >>> check_bipartite({7: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 4: [0]}) Traceback (most recent call last): ... KeyError: 0 - >>> check_bipartite( - ... {0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]} - ... ) + >>> check_bipartite({0: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2], 9: [0]}) Traceback (most recent call last): ... KeyError: 4 - >>> check_bipartite( - ... {0: [-1, 3], 1: [0, -2]} - ... ) + >>> check_bipartite({0: [-1, 3], 1: [0, -2]}) Traceback (most recent call last): ... IndexError: list index out of range - >>> check_bipartite( - ... {-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]} - ... ) + >>> check_bipartite({-1: [0, 2], 0: [-1, 1], 1: [0, 2], 2: [-1, 1]}) True - >>> check_bipartite( - ... {0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]} - ... ) + >>> check_bipartite({0.9: [1, 3], 1: [0, 2], 2: [1, 3], 3: [0, 2]}) Traceback (most recent call last): ... KeyError: 0 - >>> check_bipartite( - ... {0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]} - ... ) + >>> check_bipartite({0: [1.0, 3.0], 1.0: [0, 2.0], 2.0: [1.0, 3.0], 3.0: [0, 2.0]}) Traceback (most recent call last): ... TypeError: list indices must be integers or slices, not float - >>> check_bipartite( - ... {"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]} - ... ) + >>> check_bipartite({"a": [1, 3], "b": [0, 2], "c": [1, 3], "d": [0, 2]}) Traceback (most recent call last): ... KeyError: 0 - >>> check_bipartite( - ... {0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]} - ... ) + >>> check_bipartite({0: ["b", "d"], 1: ["a", "c"], 2: ["b", "d"], 3: ["a", "c"]}) Traceback (most recent call last): ... TypeError: list indices must be integers or slices, not str