From ec56dec4c2a1f37ca26b69528120835038ec3a99 Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Mon, 6 Jan 2020 11:22:40 +0800 Subject: [PATCH 1/6] enhance swapping code in link --- data_structures/linked_list/swap_nodes.py | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index a6a50091e3e0..bf872501616f 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -53,6 +53,22 @@ def swapNodes(self, d1, d2): D1.next = D2.next D2.next = temp + def swapNodes2(self, d1, d2): + if d1 == d2: + return + else: + D1 = self.head + while D1 is not None and D1.data != d1: + D1 = D1.next + + D2 = self.head + while D2 is not None and D2.data != d2: + D2 = D2.next + + if D1 is None or D2 is None: + return + + D1.data, D2.data = D2.data, D1.data # swapping code ends here @@ -70,3 +86,7 @@ def swapNodes(self, d1, d2): list.swapNodes(1, 4) print("After swapping") list.print_list() + + list.swapNodes2(1, 4) + print("After swapping") + list.print_list() From bbfbf7ab6d7473a5a3df565b4d75c0b84f5fc6ab Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Mon, 6 Jan 2020 14:10:21 +0800 Subject: [PATCH 2/6] heapify do not recursive --- sorts/heap_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index a39ae2b88da2..80f932fd48b7 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -23,7 +23,7 @@ def heapify(unsorted, index, heap_size): if largest != index: unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] - heapify(unsorted, largest, heap_size) + # heapify(unsorted, largest, heap_size) def heap_sort(unsorted): From 2051ecb3d05a59b5f27fa84c844aa80f707fa339 Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Mon, 6 Jan 2020 14:40:47 +0800 Subject: [PATCH 3/6] fix --- sorts/heap_sort.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sorts/heap_sort.py b/sorts/heap_sort.py index 80f932fd48b7..a39ae2b88da2 100644 --- a/sorts/heap_sort.py +++ b/sorts/heap_sort.py @@ -23,7 +23,7 @@ def heapify(unsorted, index, heap_size): if largest != index: unsorted[largest], unsorted[index] = unsorted[index], unsorted[largest] - # heapify(unsorted, largest, heap_size) + heapify(unsorted, largest, heap_size) def heap_sort(unsorted): From d0c81ed1e3d62e7cbc6469f84b53f7431293575a Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Sun, 12 Jan 2020 15:45:09 +0800 Subject: [PATCH 4/6] fix identifier and add test --- data_structures/linked_list/swap_nodes.py | 69 +++++------------------ 1 file changed, 15 insertions(+), 54 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index bf872501616f..eb57b818928d 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -11,8 +11,9 @@ def __init__(self): def print_list(self): temp = self.head while temp is not None: - print(temp.data) + print(temp.data, end=' ') temp = temp.next + print() # adding nodes def push(self, new_data): @@ -21,72 +22,32 @@ def push(self, new_data): self.head = new_node # swapping nodes - def swapNodes(self, d1, d2): - prevD1 = None - prevD2 = None - if d1 == d2: + def swap_nodes(self, node_data_1, node_data_2): + if node_data_1 == node_data_2: return else: - # find d1 - D1 = self.head - while D1 is not None and D1.data != d1: - prevD1 = D1 - D1 = D1.next - # find d2 - D2 = self.head - while D2 is not None and D2.data != d2: - prevD2 = D2 - D2 = D2.next - if D1 is None and D2 is None: - return - # if D1 is head - if prevD1 is not None: - prevD1.next = D2 - else: - self.head = D2 - # if D2 is head - if prevD2 is not None: - prevD2.next = D1 - else: - self.head = D1 - temp = D1.next - D1.next = D2.next - D2.next = temp - - def swapNodes2(self, d1, d2): - if d1 == d2: - return - else: - D1 = self.head - while D1 is not None and D1.data != d1: - D1 = D1.next + node_1 = self.head + while node_1 is not None and node_1.data != node_data_1: + node_1 = node_1.next - D2 = self.head - while D2 is not None and D2.data != d2: - D2 = D2.next + node_2 = self.head + while node_2 is not None and node_2.data != node_data_2: + node_2 = node_2.next - if D1 is None or D2 is None: + if node_1 is None or node_2 is None: return - D1.data, D2.data = D2.data, D1.data + node_1.data, node_2.data = node_2.data, node_1.data -# swapping code ends here if __name__ == "__main__": list = Linkedlist() - list.push(5) - list.push(4) - list.push(3) - list.push(2) - list.push(1) + for i in range(5, 0, -1): + list.push(i) list.print_list() - list.swapNodes(1, 4) - print("After swapping") - list.print_list() - - list.swapNodes2(1, 4) + list.swap_nodes(1, 4) print("After swapping") list.print_list() From 8a09815a1610c75658ba4e9798631434980d2d91 Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Tue, 14 Jan 2020 12:10:16 +0800 Subject: [PATCH 5/6] typing.Any and LinkedList instead of Linkedlist --- data_structures/linked_list/swap_nodes.py | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index eb57b818928d..64f9ef272c43 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -1,10 +1,13 @@ +from typing import Any + + class Node: - def __init__(self, data): + def __init__(self, data: Any): self.data = data self.next = None -class Linkedlist: +class LinkedList: def __init__(self): self.head = None @@ -16,7 +19,7 @@ def print_list(self): print() # adding nodes - def push(self, new_data): + def push(self, new_data: Any): new_node = Node(new_data) new_node.next = self.head self.head = new_node @@ -40,9 +43,8 @@ def swap_nodes(self, node_data_1, node_data_2): node_1.data, node_2.data = node_2.data, node_1.data - if __name__ == "__main__": - list = Linkedlist() + list = LinkedList() for i in range(5, 0, -1): list.push(i) From 517fc1b3a49338f9264e2cfff99cc2d52b862ca8 Mon Sep 17 00:00:00 2001 From: lanzhiwang Date: Tue, 14 Jan 2020 12:14:17 +0800 Subject: [PATCH 6/6] typing.Any and LinkedList instead of Linkedlist --- data_structures/linked_list/swap_nodes.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/swap_nodes.py b/data_structures/linked_list/swap_nodes.py index 64f9ef272c43..bb177f419c30 100644 --- a/data_structures/linked_list/swap_nodes.py +++ b/data_structures/linked_list/swap_nodes.py @@ -44,12 +44,12 @@ def swap_nodes(self, node_data_1, node_data_2): if __name__ == "__main__": - list = LinkedList() + ll = LinkedList() for i in range(5, 0, -1): - list.push(i) + ll.push(i) - list.print_list() + ll.print_list() - list.swap_nodes(1, 4) + ll.swap_nodes(1, 4) print("After swapping") - list.print_list() + ll.print_list()