From 8bbe36a4764c0a342c4cbe1589112f501613f16d Mon Sep 17 00:00:00 2001 From: Chaitanya Bhopi Date: Sat, 29 Aug 2026 20:41:15 +0530 Subject: [PATCH 1/5] Add doctest to topological_sort --- sorts/topological_sort.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index efce8165fcac..c47a369dfb6b 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -16,7 +16,12 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[str]: - """Perform topological sort on a directed acyclic graph.""" + """ + Perform topological sort on a directed acyclic graph. + + >>> topological_sort('a', [], []) + ['c', 'd', 'e', 'b', 'a'] + """ current = start # add current to visited visited.append(current) From 0896d9d4c79563c03c54a6eaedfac6149d783060 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 9 Sep 2026 11:07:50 +0200 Subject: [PATCH 2/5] Apply batched suggestions from code review Co-authored-by: Christian Clauss --- sorts/topological_sort.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index c47a369dfb6b..65e6bdd780af 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -1,4 +1,10 @@ -"""Topological Sort.""" +"""Topological Sort. + +https://en.wikipedia.org/wiki/Topological_sorting +https://en.wikipedia.org/wiki/Directed_acyclic_graph + +Note: topological_sort() sorts a directed acyclic graph so topological_sort(2, 1, 3) should fail. +""" # a # / \ @@ -21,7 +27,21 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st >>> topological_sort('a', [], []) ['c', 'd', 'e', 'b', 'a'] + + >>> topological_sort("a", "b", "c") + Traceback (most recent call last): + ... + ValueError: visited must be a list" + + >>> topological_sort"a", [], "c") + Traceback (most recent call last): + ... + ValueError: visited must be a list" """ + if not isinstance(visited, list): + raise ValueError("visited must be a list") + if not isinstance(current, list): + raise ValueError("current must be a list") current = start # add current to visited visited.append(current) From 0c2863d142ec09b08636983079aac206478e61c5 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 9 Sep 2026 09:08:03 +0000 Subject: [PATCH 3/5] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- sorts/topological_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index 65e6bdd780af..d9e2badb15d3 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -36,7 +36,7 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st >>> topological_sort"a", [], "c") Traceback (most recent call last): ... - ValueError: visited must be a list" + ValueError: visited must be a list" """ if not isinstance(visited, list): raise ValueError("visited must be a list") From f89734fecf5f3ae84ea94409dca95b09efc2b583 Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 9 Sep 2026 11:13:15 +0200 Subject: [PATCH 4/5] Apply batched suggestions from code review Co-authored-by: Christian Clauss --- sorts/topological_sort.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index d9e2badb15d3..1d171e9013dd 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -3,7 +3,8 @@ https://en.wikipedia.org/wiki/Topological_sorting https://en.wikipedia.org/wiki/Directed_acyclic_graph -Note: topological_sort() sorts a directed acyclic graph so topological_sort(2, 1, 3) should fail. +Note: topological_sort() sorts a directed acyclic graph so topological_sort(2, 1, 3) + should fail. """ # a @@ -33,15 +34,15 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st ... ValueError: visited must be a list" - >>> topological_sort"a", [], "c") + >>> topological_sort("a", [], "c") Traceback (most recent call last): ... - ValueError: visited must be a list" + ValueError: sort must be a list" """ if not isinstance(visited, list): raise ValueError("visited must be a list") - if not isinstance(current, list): - raise ValueError("current must be a list") + if not isinstance(sort, list): + raise ValueError("sort must be a list") current = start # add current to visited visited.append(current) From dd15042602138c1067091e204d4ecd0898b8093e Mon Sep 17 00:00:00 2001 From: Christian Clauss Date: Wed, 9 Sep 2026 11:16:11 +0200 Subject: [PATCH 5/5] Fix error messages for input validation --- sorts/topological_sort.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/sorts/topological_sort.py b/sorts/topological_sort.py index 1d171e9013dd..4c70b436e0ec 100644 --- a/sorts/topological_sort.py +++ b/sorts/topological_sort.py @@ -32,12 +32,12 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st >>> topological_sort("a", "b", "c") Traceback (most recent call last): ... - ValueError: visited must be a list" + ValueError: visited must be a list >>> topological_sort("a", [], "c") Traceback (most recent call last): ... - ValueError: sort must be a list" + ValueError: sort must be a list """ if not isinstance(visited, list): raise ValueError("visited must be a list")