Skip to content

Commit 4b1e4f8

Browse files
committed
test: add print_reverse test
Fix doctest syntax and remove edge case tests that are covered by typed arguments. Add `print_reverse` test that expects the correct values are printed out by adding a `test_print_reverse_output` helper function.
1 parent b23348a commit 4b1e4f8

File tree

1 file changed

+31
-21
lines changed

1 file changed

+31
-21
lines changed

Diff for: data_structures/linked_list/print_reverse.py

+31-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Program to print the elements of a linked list in reverse
2-
32
from typing import List
43

4+
55
class Node:
66
def __init__(self, data=None):
77
self.data = data
@@ -20,22 +20,18 @@ def __repr__(self):
2020

2121
def make_linked_list(elements_list: List):
2222
"""Creates a Linked List from the elements of the given sequence
23-
(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
23+
(list/tuple) and returns the head of the Linked List.
24+
2925
>>> make_linked_list([])
3026
Traceback (most recent call last):
3127
...
3228
Exception: The Elements List is empty
3329
>>> make_linked_list([7])
34-
'<7> ---> <END>'
30+
<7> ---> <END>
3531
>>> make_linked_list(['abc'])
36-
'<abc> ---> <END>'
32+
<abc> ---> <END>
3733
>>> make_linked_list([7, 25])
38-
'<7> ---> <25> ---> <END>'
34+
<7> ---> <25> ---> <END>
3935
"""
4036

4137
# if elements_list is empty
@@ -55,12 +51,8 @@ def make_linked_list(elements_list: List):
5551

5652

5753
def print_reverse(head_node: Node):
58-
"""Prints the elements of the given Linked List in reverse order"""
59-
"""
60-
>>> print_reverse()
61-
None
54+
"""Prints the elements of the given Linked List in reverse order
6255
>>> print_reverse([])
63-
None
6456
"""
6557

6658
# If reached end of the List
@@ -72,9 +64,27 @@ def print_reverse(head_node: Node):
7264
print(head_node.data)
7365

7466

75-
list_data = [14, 52, 14, 12, 43]
76-
linked_list = make_linked_list(list_data)
77-
print("Linked List:")
78-
print(linked_list)
79-
print("Elements in Reverse:")
80-
print_reverse(linked_list)
67+
def test_print_reverse_output():
68+
test_list_data = [69, 88, 73]
69+
linked_list = make_linked_list(test_list_data)
70+
71+
print_reverse(linked_list)
72+
73+
74+
def main():
75+
"""
76+
>>> test_print_reverse_output()
77+
73
78+
88
79+
69
80+
"""
81+
list_data = [14, 52, 14, 12, 43]
82+
linked_list = make_linked_list(list_data)
83+
print("Linked List:")
84+
print(linked_list)
85+
print("Elements in Reverse:")
86+
print_reverse(linked_list)
87+
88+
89+
if __name__ == "__main__":
90+
main()

0 commit comments

Comments
 (0)