From d62ffe916451745b2653634ec82b13221e99a545 Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 12:23:45 +0200 Subject: [PATCH 01/15] Node is a dataclass --- data_structures/linked_list/doubly_linked_list_two.py | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index e993cc5a20af..ca3ad5c68108 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -9,12 +9,15 @@ Delete operation is more efficient """ +from dataclasses import dataclass +from typing import Any, Self + +@dataclass class Node: - def __init__(self, data: int, previous=None, next_node=None): - self.data = data - self.previous = previous - self.next = next_node + data: Any + previous: Self | None = None + next: Self | None = None def __str__(self) -> str: return f"{self.data}" From d09b1823b5776854c1fd2dff927d43f6d3ea60b2 Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 12:24:45 +0200 Subject: [PATCH 02/15] fix mypy errors --- data_structures/linked_list/doubly_linked_list_two.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index ca3ad5c68108..2bfd2b91d5ea 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -106,7 +106,7 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.next = node node_to_insert.previous = node.previous - if node.get_previous() is None: + if not node.previous: self.head = node_to_insert else: node.previous.next = node_to_insert @@ -117,7 +117,7 @@ def insert_after_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.previous = node node_to_insert.next = node.next - if node.get_next() is None: + if node.next is None: self.tail = node_to_insert else: node.next.previous = node_to_insert @@ -156,10 +156,10 @@ def delete_value(self, value): @staticmethod def remove_node_pointers(node: Node) -> None: - if node.get_next(): + if node.next: node.next.previous = node.previous - if node.get_previous(): + if node.previous: node.previous.next = node.next node.next = None From 2d0e2a4b97062704514ca7a7c3309d1b94dd75dd Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 12:29:02 +0200 Subject: [PATCH 03/15] LinkedList is a dataclass --- data_structures/linked_list/doubly_linked_list_two.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 2bfd2b91d5ea..8a053af51277 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -48,10 +48,10 @@ def __next__(self): return value +@dataclass class LinkedList: - def __init__(self): - self.head = None # First node in list - self.tail = None # Last node in list + head: Node | None = None + tail: Node | None = None def __str__(self): current = self.head From bb26048718be8f7dc2114315c42fdc4681da14de Mon Sep 17 00:00:00 2001 From: Isidro Arias Date: Mon, 31 Mar 2025 12:34:52 +0200 Subject: [PATCH 04/15] fix mypy errors --- data_structures/linked_list/doubly_linked_list_two.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 8a053af51277..bd9083fd317c 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -93,6 +93,7 @@ def set_tail(self, node: Node) -> None: if self.head is None: self.set_head(node) else: + assert self.tail self.insert_after_node(self.tail, node) def insert(self, value: int) -> None: @@ -134,6 +135,8 @@ def insert_at_position(self, position: int, value: int) -> None: return current_position += 1 node = node.next + if not self.tail: + raise IndexError(position) self.insert_after_node(self.tail, new_node) def get_node(self, item: int) -> Node: From 13b560c40f892c4eb4f5210c75fd23fa3f821fc1 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:19:07 +0300 Subject: [PATCH 05/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index bd9083fd317c..e4894dd5dbf6 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -10,12 +10,12 @@ """ from dataclasses import dataclass -from typing import Any, Self +from typing import Self @dataclass class Node: - data: Any + data: int previous: Self | None = None next: Self | None = None From c5bd6360cbfaf9ff47c07bc4690d8c24626ef61f Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:25:04 +0300 Subject: [PATCH 06/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index e4894dd5dbf6..765d69e60091 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -135,8 +135,6 @@ def insert_at_position(self, position: int, value: int) -> None: return current_position += 1 node = node.next - if not self.tail: - raise IndexError(position) self.insert_after_node(self.tail, new_node) def get_node(self, item: int) -> Node: From 767cbf3ef5f2c1811262ac9b14cb8a7c3a2b55f4 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:29:54 +0300 Subject: [PATCH 07/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 9 --------- 1 file changed, 9 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 765d69e60091..b08afbdc8ed7 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -22,15 +22,6 @@ class Node: def __str__(self) -> str: return f"{self.data}" - def get_data(self) -> int: - return self.data - - def get_next(self): - return self.next - - def get_previous(self): - return self.previous - class LinkedListIterator: def __init__(self, head): From 7a62b87399f647cc143d081f31c2f31a1e45c419 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:31:21 +0300 Subject: [PATCH 08/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 1 - 1 file changed, 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index b08afbdc8ed7..2f9c9d5867ff 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -84,7 +84,6 @@ def set_tail(self, node: Node) -> None: if self.head is None: self.set_head(node) else: - assert self.tail self.insert_after_node(self.tail, node) def insert(self, value: int) -> None: From 5761089a166feb8f7794cba0e0a737f182ebcb4f Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:32:37 +0300 Subject: [PATCH 09/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 2f9c9d5867ff..62bbd743fce7 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -41,8 +41,8 @@ def __next__(self): @dataclass class LinkedList: - head: Node | None = None - tail: Node | None = None + head: Node | None = None # First node in list + tail: Node | None = None # Last node in list def __str__(self): current = self.head From 43809bee2a2914d19f0bbfbb238e685bd8ca0df7 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:34:03 +0300 Subject: [PATCH 10/15] Update doubly_linked_list_two.py --- .../linked_list/doubly_linked_list_two.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 62bbd743fce7..8b2084c314d2 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -34,7 +34,7 @@ def __next__(self): if not self.current: raise StopIteration else: - value = self.current.get_data() + value = self.current.data self.current = self.current.get_next() return value @@ -48,14 +48,14 @@ def __str__(self): current = self.head nodes = [] while current is not None: - nodes.append(current.get_data()) + nodes.append(current.data) current = current.get_next() return " ".join(str(node) for node in nodes) def __contains__(self, value: int): current = self.head while current: - if current.get_data() == value: + if current.data == value: return True current = current.get_next() return False @@ -65,12 +65,12 @@ def __iter__(self): def get_head_data(self): if self.head: - return self.head.get_data() + return self.head.data return None def get_tail_data(self): if self.tail: - return self.tail.get_data() + return self.tail.data return None def set_head(self, node: Node) -> None: @@ -130,7 +130,7 @@ def insert_at_position(self, position: int, value: int) -> None: def get_node(self, item: int) -> Node: node = self.head while node: - if node.get_data() == item: + if node.data == item: return node node = node.get_next() raise Exception("Node not found") From 6aeb5b8bee934c8850f0331dd25c4bd82febe0d8 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:34:33 +0300 Subject: [PATCH 11/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 8b2084c314d2..4fb83ba4089a 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -141,7 +141,7 @@ def delete_value(self, value): self.head = self.head.get_next() if node == self.tail: - self.tail = self.tail.get_previous() + self.tail = self.tail.previous self.remove_node_pointers(node) From 1d05c39f38d03bce709898488cfe787348ece2a1 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:35:17 +0300 Subject: [PATCH 12/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 4fb83ba4089a..740e7d58acc0 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -35,7 +35,7 @@ def __next__(self): raise StopIteration else: value = self.current.data - self.current = self.current.get_next() + self.current = self.current.next return value @@ -49,7 +49,7 @@ def __str__(self): nodes = [] while current is not None: nodes.append(current.data) - current = current.get_next() + current = current.next return " ".join(str(node) for node in nodes) def __contains__(self, value: int): @@ -57,7 +57,7 @@ def __contains__(self, value: int): while current: if current.data == value: return True - current = current.get_next() + current = current.next return False def __iter__(self): @@ -132,13 +132,13 @@ def get_node(self, item: int) -> Node: while node: if node.data == item: return node - node = node.get_next() + node = node.next raise Exception("Node not found") def delete_value(self, value): if (node := self.get_node(value)) is not None: if node == self.head: - self.head = self.head.get_next() + self.head = self.head.next if node == self.tail: self.tail = self.tail.previous From 995e8fd8f49549171214413383312cacc0153650 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:43:01 +0300 Subject: [PATCH 13/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 740e7d58acc0..732f133b8cc5 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -97,7 +97,7 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node_to_insert.next = node node_to_insert.previous = node.previous - if not node.previous: + if node.previous is None: self.head = node_to_insert else: node.previous.next = node_to_insert From d6b89e883ecdb6567149172dac8c54f892196e10 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:45:40 +0300 Subject: [PATCH 14/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 732f133b8cc5..76c5610bfcc0 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -104,7 +104,7 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node.previous = node_to_insert - def insert_after_node(self, node: Node, node_to_insert: Node) -> None: + def insert_after_node(self, node: Node | None, node_to_insert: Node) -> None: node_to_insert.previous = node node_to_insert.next = node.next From 7a665e362ebad62036a189562d5d5f3bdf908b73 Mon Sep 17 00:00:00 2001 From: Maxim Smolskiy Date: Tue, 1 Apr 2025 21:52:29 +0300 Subject: [PATCH 15/15] Update doubly_linked_list_two.py --- data_structures/linked_list/doubly_linked_list_two.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/data_structures/linked_list/doubly_linked_list_two.py b/data_structures/linked_list/doubly_linked_list_two.py index 76c5610bfcc0..3d3bfb0cde30 100644 --- a/data_structures/linked_list/doubly_linked_list_two.py +++ b/data_structures/linked_list/doubly_linked_list_two.py @@ -105,6 +105,8 @@ def insert_before_node(self, node: Node, node_to_insert: Node) -> None: node.previous = node_to_insert def insert_after_node(self, node: Node | None, node_to_insert: Node) -> None: + assert node is not None + node_to_insert.previous = node node_to_insert.next = node.next