diff --git a/data_structures/binary_tree/binary_search_tree.py b/data_structures/binary_tree/binary_search_tree.py index ce490fd98524..c65c71795657 100644 --- a/data_structures/binary_tree/binary_search_tree.py +++ b/data_structures/binary_tree/binary_search_tree.py @@ -152,7 +152,8 @@ def inorder(self, arr: list, node: Node): def find_kth_smallest(self, k: int, node: Node) -> int: """Return the kth smallest element in a binary search tree""" arr: list = [] - self.inorder(arr, node) # append all values to list using inorder traversal + # append all values to list using inorder traversal + self.inorder(arr, node) return arr[k - 1] @@ -186,36 +187,35 @@ def binary_search_tree(): Traceback (most recent call last): ... IndexError: Warning: Tree is empty! please use another. + >>> t = BinarySearchTree().insert(8, 3, 6, 1, 10, 14, 13, 4, 7) + >>> t.search(6) + {'6': (4, 7)} + >>> t.search(-1) + >>> print(t.root.value) + 8 + >>> print(t) + {'8': ({'3': (1, {'6': (4, 7)})}, {'10': (None, {'14': (13, None)})})} + >>> t.get_max().value + 14 + >>> t.get_min().value + 1 + >>> t.find_kth_smallest(k=2,node=t.root) + 3 + >>> t.find_kth_smallest(k=5,node=t.root) + 7 + >>> t.remove(7) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 8 3 1 6 4 10 14 13 + >>> t.remove(10) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 8 3 1 6 4 14 13 + >>> t.remove(14) + >>> print(" ".join(repr(i.value) for i in t.traversal_tree())) + 8 3 1 6 4 13 """ - testlist = (8, 3, 6, 1, 10, 14, 13, 4, 7) - t = BinarySearchTree() - for i in testlist: - t.insert(i) - - # Prints all the elements of the list in order traversal - print(t) - - if t.search(6) is not None: - print("The value 6 exists") - else: - print("The value 6 doesn't exist") - - if t.search(-1) is not None: - print("The value -1 exists") - else: - print("The value -1 doesn't exist") - - if not t.empty(): - print("Max Value: ", t.get_max().value) - print("Min Value: ", t.get_min().value) - - for i in testlist: - t.remove(i) - print(t) if __name__ == "__main__": import doctest doctest.testmod() - # binary_search_tree() diff --git a/data_structures/queue/circular_queue.py b/data_structures/queue/circular_queue.py index 93a6ef805c7c..e8321c79c6f4 100644 --- a/data_structures/queue/circular_queue.py +++ b/data_structures/queue/circular_queue.py @@ -92,3 +92,9 @@ def dequeue(self): self.front = (self.front + 1) % self.n self.size -= 1 return temp + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/queue/linked_queue.py b/data_structures/queue/linked_queue.py index 21970e7df965..74d02fb5500c 100644 --- a/data_structures/queue/linked_queue.py +++ b/data_structures/queue/linked_queue.py @@ -149,6 +149,6 @@ def clear(self) -> None: if __name__ == "__main__": - from doctest import testmod + import doctest - testmod() + doctest.testmod() diff --git a/data_structures/queue/priority_queue_using_list.py b/data_structures/queue/priority_queue_using_list.py index c5cf26433fff..3387457356ba 100644 --- a/data_structures/queue/priority_queue_using_list.py +++ b/data_structures/queue/priority_queue_using_list.py @@ -59,7 +59,7 @@ class FixedPriorityQueue: >>> fpq.dequeue() Traceback (most recent call last): ... - data_structures.queue.priority_queue_using_list.UnderFlowError: All queues are empty + priority_queue_using_list.UnderFlowError: All queues are empty >>> print(fpq) Priority 0: [] Priority 1: [] @@ -78,6 +78,21 @@ def enqueue(self, priority: int, data: int) -> None: Add an element to a queue based on its priority. If the priority is invalid ValueError is raised. If the queue is full an OverFlowError is raised. + + Examples + Those test should fail and raise Overflowerror and value exception. + + >>> fpq = FixedPriorityQueue() + >>> fpq.enqueue(3, 10) + Traceback (most recent call last): + ... + ValueError: Valid priorities are 0, 1, and 2 + >>> fpq = FixedPriorityQueue() + >>> q = [fpq.enqueue(0,5) for _ in range(101)] + Traceback (most recent call last): + ... + OverflowError: Maximum queue size is 100 + """ try: if len(self.queues[priority]) >= 100: @@ -141,9 +156,16 @@ class ElementPriorityQueue: >>> epq.dequeue() Traceback (most recent call last): ... - data_structures.queue.priority_queue_using_list.UnderFlowError: The queue is empty + priority_queue_using_list.UnderFlowError: The queue is empty >>> print(epq) [] + + + >>> epq2 = ElementPriorityQueue() + >>> q = [[epq2.enqueue(11)] for _ in range(101)] + Traceback (most recent call last): + ... + priority_queue_using_list.OverFlowError: Maximum queue size is 100 """ def __init__(self): @@ -153,6 +175,7 @@ def enqueue(self, data: int) -> None: """ This function enters the element into the queue If the queue is full an Exception is raised saying Over Flow! + """ if len(self.queue) == 100: raise OverFlowError("Maximum queue size is 100") @@ -177,56 +200,7 @@ def __str__(self) -> str: return str(self.queue) -def fixed_priority_queue(): - fpq = FixedPriorityQueue() - fpq.enqueue(0, 10) - fpq.enqueue(1, 70) - fpq.enqueue(0, 100) - fpq.enqueue(2, 1) - fpq.enqueue(2, 5) - fpq.enqueue(1, 7) - fpq.enqueue(2, 4) - fpq.enqueue(1, 64) - fpq.enqueue(0, 128) - print(fpq) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - print(fpq.dequeue()) - - -def element_priority_queue(): - epq = ElementPriorityQueue() - epq.enqueue(10) - epq.enqueue(70) - epq.enqueue(100) - epq.enqueue(1) - epq.enqueue(5) - epq.enqueue(7) - epq.enqueue(4) - epq.enqueue(64) - epq.enqueue(128) - print(epq) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - print(epq.dequeue()) - - if __name__ == "__main__": - fixed_priority_queue() - element_priority_queue() + import doctest + + doctest.testmod() diff --git a/data_structures/queue/queue_on_list.py b/data_structures/queue/queue_on_list.py index 485cf0b6f7a3..94d0eaf9e6e9 100644 --- a/data_structures/queue/queue_on_list.py +++ b/data_structures/queue/queue_on_list.py @@ -50,3 +50,9 @@ def get_front(self): def size(self): return self.length + + +if __name__ == "__main__": + import doctest + + doctest.testmod() diff --git a/data_structures/queue/queue_on_pseudo_stack.py b/data_structures/queue/queue_on_pseudo_stack.py index 7fa2fb2566af..5fa05c5d598b 100644 --- a/data_structures/queue/queue_on_pseudo_stack.py +++ b/data_structures/queue/queue_on_pseudo_stack.py @@ -7,6 +7,18 @@ def __init__(self): self.length = 0 def __str__(self): + """ + >>> q = Queue() + >>> q.put(3) + >>> q.put(5) + >>> q.put(7) + >>> print(q) + <3, 5, 7> + + >>> q = Queue() + >>> print(q) + <> + """ printed = "<" + str(self.stack)[1:-1] + ">" return printed @@ -15,6 +27,12 @@ def __str__(self): item to enqueue""" def put(self, item): + """ + >>> q = Queue() + >>> q.put(3) + >>> assert q.stack == [3] + >>> assert q.length == 1 + """ self.stack.append(item) self.length = self.length + 1 @@ -24,6 +42,37 @@ def put(self, item): item that was dequeued""" def get(self): + """ + Testing get() and inner call to rotate() + + >>> q = Queue() + >>> q.put(3) + >>> q.put(5) + >>> q.put(7) + >>> n = q.get() + >>> assert n == 3 + >>> assert q.stack == [5,7] + + >>> q2 = Queue() + >>> q2.put(3) + >>> n = q2.get() + >>> n = q2.get() + Traceback (most recent call last): + ... + IndexError: The queue is empty + + >>> q3 = Queue() + >>> n = q3.get() + Traceback (most recent call last): + ... + IndexError: The queue is empty + + >>> q4 = Queue() + >>> q4.put(3) + >>> n = q4.get() + >>> assert n == 3 + """ + self.rotate(1) dequeued = self.stack[self.length - 1] self.stack = self.stack[:-1] @@ -33,10 +82,22 @@ def get(self): """Rotates the queue {@code rotation} times @param rotation + @requirement: |self.length| > 0 number of times to rotate queue""" def rotate(self, rotation): + """ + >>> q = Queue() + >>> q.put(3) + >>> q.put(5) + >>> q.put(7) + >>> q.rotate(2) + >>> assert q.stack == [7,3,5] + """ + for i in range(rotation): + if self.length == 0: + raise IndexError("The queue is empty") temp = self.stack[0] self.stack = self.stack[1:] self.put(temp) @@ -46,6 +107,18 @@ def rotate(self, rotation): @return item at front of self.stack""" def front(self): + """ + >>> q = Queue() + >>> q.put(3) + >>> n = q.front() + >>> assert n == 3 + + >>> q2 = Queue() + >>> n = q2.front() + Traceback (most recent call last): + ... + IndexError: The queue is empty + """ front = self.get() self.put(front) self.rotate(self.length - 1) @@ -54,4 +127,15 @@ def front(self): """Returns the length of this.stack""" def size(self): + """ + >>> q = Queue() + >>> q.size() + 0 + """ return self.length + + +if __name__ == "__main__": + import doctest + + doctest.testmod()