Skip to content

Commit b23348a

Browse files
committed
fix: add type checking and argument typing
Add argument typing to all methods in `print_reverse` Add doctest to helper function `make_linked_list` and basic edge case tests to `print_reverse`
1 parent ebf2c54 commit b23348a

File tree

1 file changed

+26
-3
lines changed

1 file changed

+26
-3
lines changed

Diff for: data_structures/linked_list/print_reverse.py

+26-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Program to print the elements of a linked list in reverse
22

3+
from typing import List
34

45
class Node:
56
def __init__(self, data=None):
@@ -17,9 +18,25 @@ def __repr__(self):
1718
return string_rep
1819

1920

20-
def make_linked_list(elements_list):
21+
def make_linked_list(elements_list: List):
2122
"""Creates a Linked List from the elements of the given sequence
2223
(list/tuple) and returns the head of the Linked List."""
24+
"""
25+
>>> make_linked_list()
26+
Traceback (most recent call last):
27+
...
28+
Exception: The Elements List is empty
29+
>>> make_linked_list([])
30+
Traceback (most recent call last):
31+
...
32+
Exception: The Elements List is empty
33+
>>> make_linked_list([7])
34+
'<7> ---> <END>'
35+
>>> make_linked_list(['abc'])
36+
'<abc> ---> <END>'
37+
>>> make_linked_list([7, 25])
38+
'<7> ---> <25> ---> <END>'
39+
"""
2340

2441
# if elements_list is empty
2542
if not elements_list:
@@ -37,11 +54,17 @@ def make_linked_list(elements_list):
3754
return head
3855

3956

40-
def print_reverse(head_node):
57+
def print_reverse(head_node: Node):
4158
"""Prints the elements of the given Linked List in reverse order"""
59+
"""
60+
>>> print_reverse()
61+
None
62+
>>> print_reverse([])
63+
None
64+
"""
4265

4366
# If reached end of the List
44-
if head_node is None:
67+
if head_node is None or not isinstance(head_node, Node):
4568
return None
4669
else:
4770
# Recurse

0 commit comments

Comments
 (0)