From f00598ce11c12725f79da85b8da7845c59309d0b Mon Sep 17 00:00:00 2001 From: John Liu Date: Sat, 26 Jul 2025 13:26:25 -0700 Subject: [PATCH 1/4] Fixes #12857 Use collections.deque as queue in graphs BFS shortest path 2 --- .../breadth_first_search_shortest_path_2.py | 22 ++++++++++--------- 1 file changed, 12 insertions(+), 10 deletions(-) diff --git a/graphs/breadth_first_search_shortest_path_2.py b/graphs/breadth_first_search_shortest_path_2.py index 4f9b6e65bdf3..1335e359f186 100644 --- a/graphs/breadth_first_search_shortest_path_2.py +++ b/graphs/breadth_first_search_shortest_path_2.py @@ -1,10 +1,12 @@ -"""Breadth-first search shortest path implementations. +"""Breadth-first search the shortest path implementations. doctest: -python -m doctest -v bfs_shortest_path.py +python -m doctest -v breadth_first_search_shortest_path_2.py Manual test: -python bfs_shortest_path.py +python breadth_first_search_shortest_path_2.py """ +from collections import deque + demo_graph = { "A": ["B", "C", "E"], "B": ["A", "D", "E"], @@ -17,7 +19,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> list[str]: - """Find shortest path between `start` and `goal` nodes. + """Find the shortest path between `start` and `goal` nodes. Args: graph (dict): node/list of neighboring nodes key/value pairs. start: start node. @@ -36,7 +38,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> list[str]: # keep track of explored nodes explored = set() # keep track of all the paths to be checked - queue = [[start]] + queue = deque([start]) # return path if start is goal if start == goal: @@ -45,7 +47,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> list[str]: # keeps looping until all possible paths have been checked while queue: # pop the first path from the queue - path = queue.pop(0) + path = queue.popleft() # get the last node from the path node = path[-1] if node not in explored: @@ -68,13 +70,13 @@ def bfs_shortest_path(graph: dict, start, goal) -> list[str]: def bfs_shortest_path_distance(graph: dict, start, target) -> int: - """Find shortest path distance between `start` and `target` nodes. + """Find the shortest path distance between `start` and `target` nodes. Args: graph: node/list of neighboring nodes key/value pairs. start: node to start search from. target: node to search for. Returns: - Number of edges in shortest path between `start` and `target` nodes. + Number of edges in the shortest path between `start` and `target` nodes. -1 if no path exists. Example: >>> bfs_shortest_path_distance(demo_graph, "G", "D") @@ -88,12 +90,12 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int: return -1 if start == target: return 0 - queue = [start] + queue = deque(start) visited = set(start) # Keep tab on distances from `start` node. dist = {start: 0, target: -1} while queue: - node = queue.pop(0) + node = queue.popleft() if node == target: dist[target] = ( dist[node] if dist[target] == -1 else min(dist[target], dist[node]) From 9f3e81c730625c071d14baa100139831de3ced9d Mon Sep 17 00:00:00 2001 From: John Liu Date: Sat, 26 Jul 2025 13:57:38 -0700 Subject: [PATCH 2/4] Use collections.deque as queue in the correct syntax: queue = deque([start]) --- graphs/breadth_first_search_shortest_path_2.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/graphs/breadth_first_search_shortest_path_2.py b/graphs/breadth_first_search_shortest_path_2.py index 1335e359f186..8bc8b6de0b12 100644 --- a/graphs/breadth_first_search_shortest_path_2.py +++ b/graphs/breadth_first_search_shortest_path_2.py @@ -90,7 +90,7 @@ def bfs_shortest_path_distance(graph: dict, start, target) -> int: return -1 if start == target: return 0 - queue = deque(start) + queue = deque([start]) visited = set(start) # Keep tab on distances from `start` node. dist = {start: 0, target: -1} From 3fe2cdd9de54891966ee2b2a7a037f7595d5905d Mon Sep 17 00:00:00 2001 From: John Liu Date: Sat, 26 Jul 2025 14:25:37 -0700 Subject: [PATCH 3/4] Fix CI error due to HTTP 404 on https://finance.yahoo.com/quote/GOOG/\?p\=GOOG --- .github/workflows/build.yml | 1 + graphs/breadth_first_search_shortest_path_2.py | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 8b83cb41c79a..8cfff35b2627 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -31,6 +31,7 @@ jobs: --ignore=quantum/q_fourier_transform.py --ignore=scripts/validate_solutions.py --ignore=web_programming/fetch_anime_and_play.py + --ignore=web_programming/current_stock_price.py --cov-report=term-missing:skip-covered --cov=. . - if: ${{ success() }} diff --git a/graphs/breadth_first_search_shortest_path_2.py b/graphs/breadth_first_search_shortest_path_2.py index 8bc8b6de0b12..efba9b7b6ae6 100644 --- a/graphs/breadth_first_search_shortest_path_2.py +++ b/graphs/breadth_first_search_shortest_path_2.py @@ -38,7 +38,7 @@ def bfs_shortest_path(graph: dict, start, goal) -> list[str]: # keep track of explored nodes explored = set() # keep track of all the paths to be checked - queue = deque([start]) + queue = deque([[start]]) # return path if start is goal if start == goal: From e005b1292996ad2fb627928910baeb254bdc9d7a Mon Sep 17 00:00:00 2001 From: John Liu Date: Tue, 29 Jul 2025 16:35:55 -0700 Subject: [PATCH 4/4] Undo change in workflows/build.yml as it's fixed in PR 12864 --- .github/workflows/build.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index b18607966785..01b67c6de05b 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -32,7 +32,6 @@ jobs: --ignore=scripts/validate_solutions.py --ignore=web_programming/current_stock_price.py --ignore=web_programming/fetch_anime_and_play.py - --ignore=web_programming/current_stock_price.py --cov-report=term-missing:skip-covered --cov=. . - if: ${{ success() }}