From 53ffad144a8c6243643c4ee386dc940bdebca89f Mon Sep 17 00:00:00 2001 From: prshnt19 Date: Wed, 4 Mar 2020 16:53:41 +0530 Subject: [PATCH 1/4] issue 124 need to be solved first --- .../miscellaneous_data_structures/queue.py | 50 ++++++++++++++++++- .../tests/test_queue.py | 17 +++++++ 2 files changed, 65 insertions(+), 2 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index 16502a571..7343615f6 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -1,5 +1,5 @@ -from pydatastructs.linear_data_structures import DynamicOneDimensionalArray -from pydatastructs.utils.misc_util import NoneType +from pydatastructs.linear_data_structures import DynamicOneDimensionalArray, SinglyLinkedList +from pydatastructs.utils.misc_util import NoneType, LinkedListNode from copy import deepcopy as dc __all__ = [ @@ -51,6 +51,12 @@ def __new__(cls, implementation='array', **kwargs): return ArrayQueue( kwargs.get('items', None), kwargs.get('dtype', int)) + + elif implementation == 'linkedlist': + return LinkedListQueue( + # kwargs.get('items', None), + # kwargs.get('dtype', LinkedListNode) + ) raise NotImplementedError( "%s hasn't been implemented yet."%(implementation)) @@ -66,6 +72,7 @@ def popleft(self, *args, **kwargs): def is_empty(self): return None + class ArrayQueue(Queue): __slots__ = ['front'] @@ -122,3 +129,42 @@ def __str__(self): for i in range(self.front, self.rear + 1): _data.append(self.items._data[i]) return str(_data) + + +class LinkedListQueue(Queue): + + __slots__ = ['front', 'rear', 'size'] + + def __new__(cls): + obj = object.__new__(cls) + obj.queue = SinglyLinkedList() + obj.front = None + obj.rear = None + obj.size = 0 + return obj + + def append(self, x): + self.size += 1 + self.queue.append(x) + if self.front == None: + self.front = self.queue.head + self.rear = self.queue.tail + + def popleft(self): + if self.is_empty: + raise ValueError("Queue is empty.") + self.size -= 1 + return_value = self.queue.pop_left() + self.front = self.queue.head + self.rear = self.queue.tail + return return_value + + @property + def is_empty(self): + return self.size == 0 + + def __len__(self): + return self.size + + def __str__(self): + return str(self.queue) diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py index 18f8030f0..10e6b7e09 100644 --- a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py +++ b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py @@ -18,3 +18,20 @@ def test_Queue(): q1 = Queue() raises(ValueError, lambda: q1.popleft()) + + q1 = Queue(implementation='linkedlist') + q1.append(0) + q1.append(1) + q1.append(2) + q1.append(3) + assert str(q1) == '[0, 1, 2, 3]' + assert len(q1) == 4 + assert q1.popleft() == 0 + assert q1.popleft() == 1 + assert len(q1) == 2 + assert q1.popleft() == 2 + assert q1.popleft() == 3 + assert len(q1) == 0 + + q1 = Queue() + raises(ValueError, lambda: q1.popleft()) From 470a83152ecf4a137d3b6d4d2b38034bc4623f70 Mon Sep 17 00:00:00 2001 From: prshnt19 Date: Fri, 6 Mar 2020 13:52:13 +0530 Subject: [PATCH 2/4] LinkedListQueue Added --- pydatastructs/miscellaneous_data_structures/queue.py | 3 +-- .../miscellaneous_data_structures/tests/test_queue.py | 8 ++++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index 7343615f6..2f53625a6 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -51,7 +51,6 @@ def __new__(cls, implementation='array', **kwargs): return ArrayQueue( kwargs.get('items', None), kwargs.get('dtype', int)) - elif implementation == 'linkedlist': return LinkedListQueue( # kwargs.get('items', None), @@ -146,7 +145,7 @@ def __new__(cls): def append(self, x): self.size += 1 self.queue.append(x) - if self.front == None: + if self.front is None: self.front = self.queue.head self.rear = self.queue.tail diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py index 10e6b7e09..5bfa37f40 100644 --- a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py +++ b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py @@ -26,11 +26,11 @@ def test_Queue(): q1.append(3) assert str(q1) == '[0, 1, 2, 3]' assert len(q1) == 4 - assert q1.popleft() == 0 - assert q1.popleft() == 1 + assert q1.popleft().data == 0 + assert q1.popleft().data == 1 assert len(q1) == 2 - assert q1.popleft() == 2 - assert q1.popleft() == 3 + assert q1.popleft().data == 2 + assert q1.popleft().data == 3 assert len(q1) == 0 q1 = Queue() From 4c2a62d973aebf492c348f01bdba337e2229f0dd Mon Sep 17 00:00:00 2001 From: prshnt19 Date: Fri, 6 Mar 2020 14:39:36 +0530 Subject: [PATCH 3/4] Codecov Improved --- pydatastructs/miscellaneous_data_structures/queue.py | 3 ++- .../miscellaneous_data_structures/tests/test_queue.py | 2 -- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index 2f53625a6..0afec6c07 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -69,7 +69,8 @@ def popleft(self, *args, **kwargs): @property def is_empty(self): - return None + raise NotImplementedError( + "This is an abstract method.") class ArrayQueue(Queue): diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py index 5bfa37f40..05e99949d 100644 --- a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py +++ b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py @@ -32,6 +32,4 @@ def test_Queue(): assert q1.popleft().data == 2 assert q1.popleft().data == 3 assert len(q1) == 0 - - q1 = Queue() raises(ValueError, lambda: q1.popleft()) From 935c8533561581df9a8ca9841990ec4bdb9a6fa7 Mon Sep 17 00:00:00 2001 From: prshnt19 Date: Sat, 7 Mar 2020 02:57:35 +0530 Subject: [PATCH 4/4] LinkedListQueue made analogous to ArrayQueue --- .../miscellaneous_data_structures/queue.py | 38 ++++++++++++------- .../tests/test_queue.py | 5 ++- 2 files changed, 29 insertions(+), 14 deletions(-) diff --git a/pydatastructs/miscellaneous_data_structures/queue.py b/pydatastructs/miscellaneous_data_structures/queue.py index 0afec6c07..e7e176fd0 100644 --- a/pydatastructs/miscellaneous_data_structures/queue.py +++ b/pydatastructs/miscellaneous_data_structures/queue.py @@ -15,17 +15,12 @@ class Queue(object): implementation : str Implementation to be used for queue. By default, 'array' - Currently only supports 'array' - implementation. items : list/tuple Optional, by default, None The inital items in the queue. - For array implementation. dtype : A valid python type Optional, by default NoneType if item - is None, otherwise takes the data - type of DynamicOneDimensionalArray - For array implementation. + is None. Examples ======== @@ -53,8 +48,8 @@ def __new__(cls, implementation='array', **kwargs): kwargs.get('dtype', int)) elif implementation == 'linkedlist': return LinkedListQueue( - # kwargs.get('items', None), - # kwargs.get('dtype', LinkedListNode) + kwargs.get('items', None), + kwargs.get('dtype', NoneType) ) raise NotImplementedError( "%s hasn't been implemented yet."%(implementation)) @@ -133,17 +128,34 @@ def __str__(self): class LinkedListQueue(Queue): - __slots__ = ['front', 'rear', 'size'] + __slots__ = ['front', 'rear', 'size', '_dtype'] - def __new__(cls): + def __new__(cls, items=None, dtype=NoneType): obj = object.__new__(cls) obj.queue = SinglyLinkedList() - obj.front = None - obj.rear = None - obj.size = 0 + obj._dtype = dtype + if items is None: + pass + elif type(items) in (list, tuple): + if len(items) != 0 and dtype is NoneType: + obj._dtype = type(items[0]) + for x in items: + if type(x) == obj._dtype: + obj.queue.append(x) + else: + raise TypeError("Expected %s but got %s"%(obj._dtype, type(x))) + else: + raise TypeError("Expected type: list/tuple") + obj.front = obj.queue.head + obj.rear = obj.queue.tail + obj.size = obj.queue.size return obj def append(self, x): + if self._dtype is NoneType: + self._dtype = type(x) + elif type(x) is not self._dtype: + raise TypeError("Expected %s but got %s"%(self._dtype, type(x))) self.size += 1 self.queue.append(x) if self.front is None: diff --git a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py index 05e99949d..49d52fe11 100644 --- a/pydatastructs/miscellaneous_data_structures/tests/test_queue.py +++ b/pydatastructs/miscellaneous_data_structures/tests/test_queue.py @@ -20,8 +20,11 @@ def test_Queue(): raises(ValueError, lambda: q1.popleft()) q1 = Queue(implementation='linkedlist') - q1.append(0) q1.append(1) + assert raises(TypeError, lambda: q1.append('a')) + assert raises(TypeError, lambda: Queue(implementation='linkedlist', items=[0], dtype=str)) + assert raises(TypeError, lambda: Queue(implementation='linkedlist', items={0, 1})) + q1 = Queue(implementation='linkedlist', items = [0, 1]) q1.append(2) q1.append(3) assert str(q1) == '[0, 1, 2, 3]'