From be1244ea463dfb3d1d8e10007a3e285b877f0e66 Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Thu, 11 Oct 2018 17:28:20 +0100 Subject: [PATCH 1/3] [Issue 34]fixed bug in merge_sorted_list --- python/07_linkedlist/linked_list_algo.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/07_linkedlist/linked_list_algo.py b/python/07_linkedlist/linked_list_algo.py index 9427126..4d69ae8 100644 --- a/python/07_linkedlist/linked_list_algo.py +++ b/python/07_linkedlist/linked_list_algo.py @@ -55,7 +55,7 @@ def merge_sorted_list(l1: Node, l2: Node) -> Optional[Node]: current = current._next current._next = p1 if p1 else p2 return fake_head._next - return p1 or p2 + return l1 or l2 # Remove nth node from the end # 删除倒数第n个节点。假设n大于0 From 01ae89d478c51703914e1a9b161c1b8c4471c476 Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Thu, 11 Oct 2018 17:46:43 +0100 Subject: [PATCH 2/3] [Issue 49]fix bug of deleting and then insert to tail --- python/05_array/myarray.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/python/05_array/myarray.py b/python/05_array/myarray.py index af55480..e4df25b 100644 --- a/python/05_array/myarray.py +++ b/python/05_array/myarray.py @@ -39,7 +39,10 @@ class MyArray: def insert_to_tail(self, value: int) -> bool: if self._count == self._capacity: return False - self._data.append(value) + if self._count == len(self._data): + self._data.append(value) + else: + self._data[self._count] = value self._count += 1 return True @@ -57,4 +60,6 @@ if __name__ == "__main__": a.insert_to_tail(i) a.delete(2) + print(a) + a.insert_to_tail(7) print(a) \ No newline at end of file From 1c6b5e932ff487f7cd26a1c1bdd7185d44b5f403 Mon Sep 17 00:00:00 2001 From: Wenru Dong Date: Thu, 11 Oct 2018 20:03:29 +0100 Subject: [PATCH 3/3] implementation of DynamicArray and queue based upon linked list in python --- python/09_queue/dynamic_array_queue.py | 51 ++++++++++++++++++++++ python/09_queue/linked_queue.py | 58 ++++++++++++++++++++++++++ 2 files changed, 109 insertions(+) create mode 100644 python/09_queue/dynamic_array_queue.py create mode 100644 python/09_queue/linked_queue.py diff --git a/python/09_queue/dynamic_array_queue.py b/python/09_queue/dynamic_array_queue.py new file mode 100644 index 0000000..b422840 --- /dev/null +++ b/python/09_queue/dynamic_array_queue.py @@ -0,0 +1,51 @@ +""" + Author: Wenru +""" + +from typing import Optional + +class DynamicArrayQueue: + + def __init__(self, capacity: int): + self._items = [] + self._capacity = capacity + self._head = 0 + self._tail = 0 + + def enqueue(self, item: str) -> bool: + if self._tail == self._capacity: + if self._head == 0: return False + + self._items[0 : self._tail - self._head] = self._items[self._head : self._tail] + self._tail -= self._head + self._head = 0 + + if self._tail == len(self._items): + self._items.append(item) + else: + self._items[self._tail] = item + self._tail += 1 + return True + + def dequeue(self) -> Optional[str]: + if self._head != self._tail: + item = self._items[self._head] + self._head += 1 + return item + + def __repr__(self) -> str: + return " ".join(item for item in self._items[self._head:self._tail]) + +if __name__ == "__main__": + q = DynamicArrayQueue(10) + for i in range(10): + q.enqueue(str(i)) + print(q) + + for _ in range(3): + q.dequeue() + print(q) + + q.enqueue("7") + q.enqueue("8") + print(q) \ No newline at end of file diff --git a/python/09_queue/linked_queue.py b/python/09_queue/linked_queue.py new file mode 100644 index 0000000..4ee4ecf --- /dev/null +++ b/python/09_queue/linked_queue.py @@ -0,0 +1,58 @@ +""" + Queue based upon linked list + + Author: Wenru +""" + +from typing import Optional + +class Node: + + def __init__(self, data: str, next=None): + self.data = data + self._next = next + +class LinkedQueue: + + def __init__(self): + self._head: Optional[Node] = None + self._tail: Optional[Node] = None + + def enqueue(self, value: str): + new_node = Node(value) + if self._tail: + self._tail._next = new_node + else: + self._head = new_node + self._tail = new_node + + def dequeue(self) -> Optional[str]: + if self._head: + value = self._head.data + self._head = self._head._next + if not self._head: + self._tail = None + return value + + def __repr__(self) -> str: + values = [] + current = self._head + while current: + values.append(current.data) + current = current._next + return "->".join(value for value in values) + + +if __name__ == "__main__": + q = LinkedQueue() + for i in range(10): + q.enqueue(str(i)) + print(q) + + for _ in range(3): + q.dequeue() + print(q) + + q.enqueue("7") + q.enqueue("8") + print(q)