Skip to content

Commit f1c37e3

Browse files
committed
fix: honor empty-list contract for generator input
make_linked_list declared the input as Iterable[int] but tested emptiness with truthiness, so an empty generator silently produced an empty LinkedList instead of raising. Materialize the iterable first and cover generator input with doctests.
1 parent 215c1f0 commit f1c37e3

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

‎data_structures/linked_list/print_reverse.py‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,14 @@ def make_linked_list(elements_list: Iterable[int]) -> LinkedList:
9898
abc
9999
>>> make_linked_list([7, 25])
100100
7 -> 25
101+
>>> make_linked_list(x for x in [7, 25])
102+
7 -> 25
103+
>>> make_linked_list(x for x in range(0))
104+
Traceback (most recent call last):
105+
...
106+
Exception: The Elements List is empty
101107
"""
108+
elements_list = list(elements_list)
102109
if not elements_list:
103110
raise Exception("The Elements List is empty")
104111

0 commit comments

Comments
 (0)