Update Linked List from sequence script to use doctests (#12766)
* Update comments for linked list script. * Add doctests for the linked list script. * Update from_sequence.py --------- Co-authored-by: Maxim Smolskiy <mithridatus@mail.ru>
This commit is contained in:
@@ -1,5 +1,7 @@
|
||||
# Recursive Program to create a Linked List from a sequence and
|
||||
# print a string representation of it.
|
||||
"""
|
||||
Recursive Program to create a Linked List from a sequence and
|
||||
print a string representation of it.
|
||||
"""
|
||||
|
||||
|
||||
class Node:
|
||||
@@ -18,13 +20,32 @@ class Node:
|
||||
return string_rep
|
||||
|
||||
|
||||
def make_linked_list(elements_list):
|
||||
"""Creates a Linked List from the elements of the given sequence
|
||||
(list/tuple) and returns the head of the Linked List."""
|
||||
def make_linked_list(elements_list: list | tuple) -> Node:
|
||||
"""
|
||||
Creates a Linked List from the elements of the given sequence
|
||||
(list/tuple) and returns the head of the Linked List.
|
||||
|
||||
>>> make_linked_list([])
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: The Elements List is empty
|
||||
>>> make_linked_list(())
|
||||
Traceback (most recent call last):
|
||||
...
|
||||
ValueError: The Elements List is empty
|
||||
>>> make_linked_list([1])
|
||||
<1> ---> <END>
|
||||
>>> make_linked_list((1,))
|
||||
<1> ---> <END>
|
||||
>>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
|
||||
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
|
||||
>>> make_linked_list((1, 3, 5, 32, 44, 12, 43))
|
||||
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
|
||||
"""
|
||||
|
||||
# if elements_list is empty
|
||||
if not elements_list:
|
||||
raise Exception("The Elements List is empty")
|
||||
raise ValueError("The Elements List is empty")
|
||||
|
||||
# Set first element as Head
|
||||
head = Node(elements_list[0])
|
||||
@@ -34,11 +55,3 @@ def make_linked_list(elements_list):
|
||||
current.next = Node(data)
|
||||
current = current.next
|
||||
return head
|
||||
|
||||
|
||||
list_data = [1, 3, 5, 32, 44, 12, 43]
|
||||
print(f"List: {list_data}")
|
||||
print("Creating Linked List from List.")
|
||||
linked_list = make_linked_list(list_data)
|
||||
print("Linked List:")
|
||||
print(linked_list)
|
||||
|
||||
Reference in New Issue
Block a user