Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit d927d67

Browse files
mindauglMaximSmolskiy
andauthored
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>
1 parent 37b34c2 commit d927d67

File tree

1 file changed

+27
-14
lines changed

1 file changed

+27
-14
lines changed

‎data_structures/linked_list/from_sequence.py

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
1-
# Recursive Program to create a Linked List from a sequence and
2-
# print a string representation of it.
1+
"""
2+
Recursive Program to create a Linked List from a sequence and
3+
print a string representation of it.
4+
"""
35

46

57
class Node:
@@ -18,13 +20,32 @@ def __repr__(self):
1820
return string_rep
1921

2022

21-
def make_linked_list(elements_list):
22-
"""Creates a Linked List from the elements of the given sequence
23-
(list/tuple) and returns the head of the Linked List."""
23+
def make_linked_list(elements_list: list | tuple) -> Node:
24+
"""
25+
Creates a Linked List from the elements of the given sequence
26+
(list/tuple) and returns the head of the Linked List.
27+
28+
>>> make_linked_list([])
29+
Traceback (most recent call last):
30+
...
31+
ValueError: The Elements List is empty
32+
>>> make_linked_list(())
33+
Traceback (most recent call last):
34+
...
35+
ValueError: The Elements List is empty
36+
>>> make_linked_list([1])
37+
<1> ---> <END>
38+
>>> make_linked_list((1,))
39+
<1> ---> <END>
40+
>>> make_linked_list([1, 3, 5, 32, 44, 12, 43])
41+
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
42+
>>> make_linked_list((1, 3, 5, 32, 44, 12, 43))
43+
<1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> <END>
44+
"""
2445

2546
# if elements_list is empty
2647
if not elements_list:
27-
raise Exception("The Elements List is empty")
48+
raise ValueError("The Elements List is empty")
2849

2950
# Set first element as Head
3051
head = Node(elements_list[0])
@@ -34,11 +55,3 @@ def make_linked_list(elements_list):
3455
current.next = Node(data)
3556
current = current.next
3657
return head
37-
38-
39-
list_data = [1, 3, 5, 32, 44, 12, 43]
40-
print(f"List: {list_data}")
41-
print("Creating Linked List from List.")
42-
linked_list = make_linked_list(list_data)
43-
print("Linked List:")
44-
print(linked_list)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /