From 9265bd54a22b333d9ac8642339d71db57cc00862 Mon Sep 17 00:00:00 2001 From: mindaugl Date: Sat, 24 May 2025 09:57:04 +0300 Subject: [PATCH 1/2] Update comments for linked list script. --- data_structures/linked_list/from_sequence.py | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/from_sequence.py b/data_structures/linked_list/from_sequence.py index fa43f4d10e08..e78072f71d9e 100644 --- a/data_structures/linked_list/from_sequence.py +++ b/data_structures/linked_list/from_sequence.py @@ -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,9 +20,11 @@ def __repr__(self): 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) -> Node: + """ + Creates a Linked List from the elements of the given sequence + (list/tuple) and returns the head of the Linked List. + """ # if elements_list is empty if not elements_list: From a32778b3b6d97b7ee6b7dd64b24cd0f523dca07c Mon Sep 17 00:00:00 2001 From: mindaugl Date: Sat, 24 May 2025 10:02:33 +0300 Subject: [PATCH 2/2] Add doctests for the linked list script. --- data_structures/linked_list/from_sequence.py | 21 ++++++++------------ 1 file changed, 8 insertions(+), 13 deletions(-) diff --git a/data_structures/linked_list/from_sequence.py b/data_structures/linked_list/from_sequence.py index e78072f71d9e..9164a78d40f0 100644 --- a/data_structures/linked_list/from_sequence.py +++ b/data_structures/linked_list/from_sequence.py @@ -20,29 +20,24 @@ def __repr__(self): return string_rep -def make_linked_list(elements_list: list) -> Node: +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([1, 3, 5, 32, 44, 12, 43]) + <1> ---> <3> ---> <5> ---> <32> ---> <44> ---> <12> ---> <43> ---> + >>> make_linked_list([1]) + <1> ---> + >>> make_linked_list((1,)) + <1> ---> """ - # if elements_list is empty if not elements_list: raise Exception("The Elements List is empty") - - # Set first element as Head head = Node(elements_list[0]) current = head - # Loop through elements from position 1 for data in elements_list[1:]: 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)