Skip to content

Commit 0896d9d

Browse files
authored
Apply batched suggestions from code review
Co-authored-by: Christian Clauss <cclauss@me.com>
1 parent 8bbe36a commit 0896d9d

1 file changed

Lines changed: 21 additions & 1 deletion

File tree

‎sorts/topological_sort.py‎

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,10 @@
1-
"""Topological Sort."""
1+
"""Topological Sort.
2+
3+
https://en.wikipedia.org/wiki/Topological_sorting
4+
https://en.wikipedia.org/wiki/Directed_acyclic_graph
5+
6+
Note: topological_sort() sorts a directed acyclic graph so topological_sort(2, 1, 3) should fail.
7+
"""
28

39
# a
410
# / \
@@ -21,7 +27,21 @@ def topological_sort(start: str, visited: list[str], sort: list[str]) -> list[st
2127
2228
>>> topological_sort('a', [], [])
2329
['c', 'd', 'e', 'b', 'a']
30+
31+
>>> topological_sort("a", "b", "c")
32+
Traceback (most recent call last):
33+
...
34+
ValueError: visited must be a list"
35+
36+
>>> topological_sort"a", [], "c")
37+
Traceback (most recent call last):
38+
...
39+
ValueError: visited must be a list"
2440
"""
41+
if not isinstance(visited, list):
42+
raise ValueError("visited must be a list")
43+
if not isinstance(current, list):
44+
raise ValueError("current must be a list")
2545
current = start
2646
# add current to visited
2747
visited.append(current)

0 commit comments

Comments
 (0)