diff --git a/binary_search_tree/binary_search_tree.py b/binary_search_tree/binary_search_tree.py index d80d9f6282..c5a7c8e119 100644 --- a/binary_search_tree/binary_search_tree.py +++ b/binary_search_tree/binary_search_tree.py @@ -9,6 +9,13 @@ 2. Implement the `in_order_print`, `bft_print`, and `dft_print` methods on the BSTNode class. """ + +import sys +sys.path.append('../queue') +from dll_queue import dll_queue +sys.path.append('../stack') +from dll_stack import dll_Stack + class BSTNode: def __init__(self, value): self.value = value @@ -17,37 +24,104 @@ def __init__(self, value): # Insert the given value into the tree def insert(self, value): - pass + # if root is not empty and value is greater than root, place value to right of root + if (value >= self.value) and (self.right != None): + self.right.insert(value) + # if root is empty and value is greater than root, create a node and place value to right of root + if (value >= self.value) and (self.right == None): + new_node = BSTNode(value) + self.right = new_node + # if root is not empty and value is lesser than root, place value to right of root + if (value < self.value) and (self.left != None): + self.left.insert(value) + # if root is empty and value is lesser than root, create a node and place value to left of root + if (value < self.value) and (self.left == None): + new_node = BSTNode(value) + self.left = new_node # Return True if the tree contains the value # False if it does not def contains(self, target): - pass + if target == self.value: + return True + if (target>self.value): + if self.right is not None: + return self.right.contains(target) + else: + return False + if (target0: + curr_node = queue.dequeue() + print (f'Value of current node is {curr_node.value}') + if curr_node.left: + queue.enqueue(curr_node.left) + if curr_node.right: + queue.enqueue(curr_node.right) # Print the value of every node, starting with the given node, # in an iterative depth first traversal def dft_print(self): - pass + # Create a stack for the nodes + # add the first node to stack + # add the the children to the stack. keep in mind to add these in same order. + stack = dll_Stack() + stack.push(self) + + while len(stack)>0: + curr_node = stack.pop() + print(f'Value of current node is {curr_node.value}') + if curr_node.left: + stack.push(curr_node.left) + if curr_node.right: + stack.push(curr_node.right) # Stretch Goals ------------------------- # Note: Research may be required @@ -73,13 +147,13 @@ def post_order_dft(self): bst.insert(4) bst.insert(2) -bst.bft_print() +#bst.bft_print() bst.dft_print() -print("elegant methods") -print("pre order") -bst.pre_order_dft() -print("in order") -bst.in_order_dft() -print("post order") -bst.post_order_dft() +# print("elegant methods") +# print("pre order") +# bst.pre_order_dft() +# print("in order") +# bst.in_order_dft() +# print("post order") +# bst.post_order_dft() diff --git a/binary_search_tree/test_binary_search_tree.py b/binary_search_tree/test_binary_search_tree.py index 0a0cee5911..7051f0d3b9 100644 --- a/binary_search_tree/test_binary_search_tree.py +++ b/binary_search_tree/test_binary_search_tree.py @@ -85,6 +85,7 @@ def test_print_traversals(self): sys.stdout = io.StringIO() self.bst.bft_print() output = sys.stdout.getvalue() + # print (output) self.assertTrue(output == "1\n8\n5\n3\n7\n2\n4\n6\n" or output == "1\n8\n5\n7\n3\n6\n4\n2\n") diff --git a/doubly_linked_list/doubly_linked_list.py b/doubly_linked_list/doubly_linked_list.py index 6f91b43a9b..1224a1c4a4 100644 --- a/doubly_linked_list/doubly_linked_list.py +++ b/doubly_linked_list/doubly_linked_list.py @@ -1,17 +1,40 @@ -""" -Each ListNode holds a reference to its previous node -as well as its next node in the List. -""" +"""Each ListNode holds a reference to its previous node +as well as its next node in the List.""" class ListNode: def __init__(self, value, prev=None, next=None): - self.prev = prev self.value = value + self.prev = prev self.next = next - -""" -Our doubly-linked list class. It holds references to -the list's head and tail nodes. -""" + + """Wrap the given value in a ListNode and insert it + after this node. Note that this node could already + have a next node it is point to.""" + def insert_after(self, value): + current_next = self.next + self.next = ListNode(value, self, current_next) + if current_next: + current_next.prev = self.next + + """Wrap the given value in a ListNode and insert it + before this node. Note that this node could already + have a previous node it is point to.""" + def insert_before(self, value): + current_prev = self.prev + self.prev = ListNode(value, current_prev, self) + if current_prev: + current_prev.next = self.prev + + """Rearranges this ListNode's previous and next pointers + accordingly, effectively deleting this ListNode.""" + def delete(self): + if self.prev: + self.prev.next = self.next + if self.next: + self.next.prev = self.prev + + +"""Our doubly-linked list class. It holds references to +the list's head and tail nodes.""" class DoublyLinkedList: def __init__(self, node=None): self.head = node @@ -20,63 +43,177 @@ def __init__(self, node=None): def __len__(self): return self.length - - """ - Wraps the given value in a ListNode and inserts it + + """Wraps the given value in a ListNode and inserts it as the new head of the list. Don't forget to handle - the old head node's previous pointer accordingly. - """ + the old head node's previous pointer accordingly.""" def add_to_head(self, value): - pass - - """ - Removes the List's current head node, making the + #wrap given value in a node + # handle if list has a head + #handle if list has no head + new_node = ListNode(value) # make a new listnode + self.length +=1 + # increment length sicne you are adding a node + if self.head: + new_node.next=self.head + self.head.prev=new_node + self.head=new_node + else: + # check if list has no head + self.head=new_node + self.tail=new_node + + """Removes the List's current head node, making the current head's next node the new head of the List. - Returns the value of the removed Node. - """ + Returns the value of the removed Node.""" def remove_from_head(self): - pass - - """ - Wraps the given value in a ListNode and inserts it + # value = self.head.value + # self.delete(self.head) + # return value + if not self.head: + return None + value = self.head.value + # print (f'value is {value}') + #if 1 node + if (self.head==self.tail): + self.head=None + self.tail=None + + # If more than 1 node + else: + self.head = self.head.next + self.head.prev=None + + self.length -=1 + # print (f'2 value is {value}') + return value + + """Wraps the given value in a ListNode and inserts it as the new tail of the list. Don't forget to handle - the old tail node's next pointer accordingly. - """ + the old tail node's next pointer accordingly.""" def add_to_tail(self, value): - pass - - """ - Removes the List's current tail node, making the + #create new node + new_node = ListNode(value,None,None) + self.length +=1 + #there is tail + if self.tail: + self.tail.next = new_node + new_node.prev=self.tail + #print (f'prev tail val is {self.tail.value}') + #self.tail=new_node + else: + #self.tail=new_node + self.head=new_node + self.tail=new_node + #print (f'tail val is {self.tail.value}') + + + """Removes the List's current tail node, making the current tail's previous node the new tail of the List. - Returns the value of the removed Node. - """ + Returns the value of the removed Node.""" def remove_from_tail(self): - pass - - """ - Removes the input node from its current spot in the - List and inserts it as the new head node of the List. - """ - def move_to_front(self, node): - pass - """ - Removes the input node from its current spot in the - List and inserts it as the new tail node of the List. - """ + #self.delete(self.tail) + #if 0 nodes + if not self.tail: + return None + value = self.tail.value + # print (f'value is {value}') + #if 1 node + if (self.head==self.tail): + self.head=None + self.tail=None + + # If more than 1 node + else: + self.tail = self.tail.prev + self.tail.next=None + + self.length -=1 + # print (f'2 value is {value}') + return value + + """Removes the input node from its current spot in the + List and inserts it as the new head node of the List.""" + def move_to_front(self, node): + # #delete + # self.delete(node) + # #add_to_front + # node.next=self.head + # self.head.prev=node + # self.head=node + + value = node.value + self.delete(node) + self.add_to_head(value) + + """Removes the input node from its current spot in the + List and inserts it as the new tail node of the List.""" def move_to_end(self, node): - pass + # #delete + # self.delete(node) + # #add_to_tail + # self.tail = node + # node.prev=self.tail + # self.tail=node + value = node.value + self.delete(node) + self.add_to_tail(value) + - """ - Deletes the input node from the List, preserving the - order of the other elements of the List. - """ + """Removes a node from the list and handles cases where + the node was the head or the tail""" def delete(self, node): - pass + #if list is empty + if not self.head: + print ("empty list!") + return - """ - Finds and returns the maximum value of all the nodes - in the List. - """ + self.length-=1 + + #if list has 1 item + if self.head==self.tail: + self.head = self.tail = None + + # if we atleaast 2 nodes and node we want to delete is the head + if node == self.head: + self.head = node.next + self.head.prev = None + + #if we atleaast 2 nodes and node we want to delete is the tail + if node == self.tail: + self.tail = node.prev + self.tail.next = None + + else: + node.delete() + + """Returns the highest value currently in the list""" def get_max(self): - pass \ No newline at end of file + #walk through entire list + highest_val = self.head.value + curr_node = self.head + + while curr_node is not None: + if curr_node.value > highest_val: + highest_val = curr_node.value + curr_node = curr_node.next + + return highest_val + + def print_val(self): + #val = self.head.value + curr_node = self.head + + while curr_node is not None: + print (f'##{curr_node.value}\n') + curr_node = curr_node.next + + def iterate_list(self,node): + while node is not None: + print(node.value) + node = node.next + + + + diff --git a/doubly_linked_list/test_doubly_linked_list.py b/doubly_linked_list/test_doubly_linked_list.py index 3a4ace4d85..7396fa5768 100644 --- a/doubly_linked_list/test_doubly_linked_list.py +++ b/doubly_linked_list/test_doubly_linked_list.py @@ -2,6 +2,7 @@ from doubly_linked_list import ListNode from doubly_linked_list import DoublyLinkedList + class DoublyLinkedListTests(unittest.TestCase): def setUp(self): self.node = ListNode(1) @@ -48,6 +49,8 @@ def test_list_add_to_tail(self): self.assertEqual(len(self.dll), 1) self.dll.add_to_tail(30) + #self.dll.print_val() + # self.dll.iterate_list(self.dll) self.assertEqual(self.dll.tail.prev.value, 1) self.assertEqual(self.dll.tail.value, 30) self.assertEqual(len(self.dll), 2) @@ -57,14 +60,36 @@ def test_list_add_to_tail(self): self.assertEqual(self.dll.tail.value, 20) self.assertEqual(len(self.dll), 3) + def test_node_delete(self): + node_1 = ListNode(3) + node_2 = ListNode(4) + node_3 = ListNode(5) + + node_1.next = node_2 + node_2.next = node_3 + node_2.prev = node_1 + node_3.prev = node_2 + + node_2.delete() + + self.assertEqual(node_1.next, node_3) + self.assertEqual(node_3.prev, node_1) + + def test_node_insert_before(self): + self.node.insert_before(0) + self.assertEqual(self.node.prev.value, 0) + def test_list_add_to_head(self): self.assertEqual(self.dll.head.value, 1) - self.dll.add_to_head(10) self.assertEqual(self.dll.head.value, 10) self.assertEqual(self.dll.head.next.value, 1) self.assertEqual(len(self.dll), 2) + def test_node_insert_after(self): + self.node.insert_after(2) + self.assertEqual(self.node.next.value, 2) + def test_list_move_to_end(self): self.dll.add_to_head(40) self.assertEqual(self.dll.tail.value, 1) @@ -104,24 +129,18 @@ def test_list_delete(self): self.assertEqual(len(self.dll), 0) self.dll.add_to_tail(1) - self.dll.add_to_head(9) + self.dll.add_to_head(9) self.dll.add_to_tail(6) - - self.dll.delete(self.dll.head.next) - self.assertEqual(self.dll.head.value, 9) - self.assertEqual(self.dll.head.next, self.dll.tail) + self.dll.delete(self.dll.head) + self.assertEqual(self.dll.head.value, 1) self.assertEqual(self.dll.tail.value, 6) + self.assertEqual(len(self.dll), 2) self.dll.delete(self.dll.head) self.assertEqual(self.dll.head.value, 6) self.assertEqual(self.dll.tail.value, 6) self.assertEqual(len(self.dll), 1) - self.dll.delete(self.dll.head) - self.assertIsNone(self.dll.head) - self.assertIsNone(self.dll.tail) - self.assertEqual(len(self.dll), 0) - def test_get_max(self): self.assertEqual(self.dll.get_max(), 1) self.dll.add_to_tail(100) @@ -131,5 +150,6 @@ def test_get_max(self): self.dll.add_to_tail(101) self.assertEqual(self.dll.get_max(), 101) + if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/queue/Array_test_queue.py b/queue/Array_test_queue.py new file mode 100644 index 0000000000..e77e0c0795 --- /dev/null +++ b/queue/Array_test_queue.py @@ -0,0 +1,47 @@ +import unittest +from array_queue import Arr_Queue + +class QueueTests(unittest.TestCase): + def setUp(self): + self.q = Arr_Queue() + + def test_len_returns_0_for_empty_queue(self): + self.assertEqual(len(self.q), 0) + + def test_len_returns_correct_length_after_enqueue(self): + self.assertEqual(len(self.q), 0) + self.q.enqueue(2) + self.assertEqual(len(self.q), 1) + self.q.enqueue(4) + self.assertEqual(len(self.q), 2) + self.q.enqueue(6) + self.q.enqueue(8) + self.q.enqueue(10) + self.q.enqueue(12) + self.q.enqueue(14) + self.q.enqueue(16) + self.q.enqueue(18) + self.assertEqual(len(self.q), 9) + + def test_empty_dequeue(self): + self.assertIsNone(self.q.dequeue()) + self.assertEqual(len(self.q), 0) + + def test_dequeue_respects_order(self): + self.q.enqueue(100) + self.q.enqueue(101) + self.q.enqueue(105) + self.assertEqual(self.q.dequeue(), 100) + self.assertEqual(len(self.q), 2) + self.assertEqual(self.q.dequeue(), 101) + self.assertEqual(len(self.q), 1) + self.assertEqual(self.q.dequeue(), 105) + self.assertEqual(len(self.q), 0) + self.assertIsNone(self.q.dequeue()) + self.assertEqual(len(self.q), 0) + +if __name__ == '__main__': + unittest.main() + + + diff --git a/queue/queue.py b/queue/array_queue.py similarity index 74% rename from queue/queue.py rename to queue/array_queue.py index 0d2599ded7..8178b1fe86 100644 --- a/queue/queue.py +++ b/queue/array_queue.py @@ -13,16 +13,21 @@ Stretch: What if you could only use instances of your Stack class to implement the Queue? What would that look like? How many Stacks would you need? Try it! """ -class Queue: + +import array as arr +class Arr_Queue: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = arr.array('i',[]) def __len__(self): - pass + return len(self.storage) def enqueue(self, value): - pass + self.storage.extend([value]) def dequeue(self): - pass + try: + return self.storage.pop(0) + except(IndexError): + print ('Array is empty!') diff --git a/queue/dll_queue.py b/queue/dll_queue.py new file mode 100644 index 0000000000..709a4b77ac --- /dev/null +++ b/queue/dll_queue.py @@ -0,0 +1,22 @@ +import sys +sys.path.append('../doubly_linked_list') +from doubly_linked_list import DoublyLinkedList + +class dll_queue(): + def __init__(self): + self.storage = DoublyLinkedList() + self.size = len(self.storage) + + def __len__(self): + return self.size + + def enqueue(self,value): + self.storage.add_to_tail(value) + self.size +=1 + + def dequeue(self): + if self.size == 0: + return None + else: + self.size -= 1 + return self.storage.remove_from_head() \ No newline at end of file diff --git a/queue/linkedlist_queue.py b/queue/linkedlist_queue.py new file mode 100644 index 0000000000..1737457879 --- /dev/null +++ b/queue/linkedlist_queue.py @@ -0,0 +1,25 @@ +import sys +sys.path.append('../singly_linked_list') +from singly_linked_list import LinkedList + +class ll_queue: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def enqueue(self,value): + self.storage.add_to_tail(value) + self.size +=1 + + def dequeue(self): + if self.size == 0: + return None + else: + self.size -= 1 + return self.storage.remove_head() + + def printlist(self): + self.storage.print_val() \ No newline at end of file diff --git a/queue/linkedlist_test_queue.py b/queue/linkedlist_test_queue.py new file mode 100644 index 0000000000..b37c4c1f2d --- /dev/null +++ b/queue/linkedlist_test_queue.py @@ -0,0 +1,47 @@ +import unittest +from linkedlist_queue import ll_queue + +class QueueTests(unittest.TestCase): + def setUp(self): + self.q = ll_queue() + + def test_len_returns_0_for_empty_queue(self): + self.assertEqual(len(self.q), 0) + + # def test_len_returns_correct_length_after_enqueue(self): + # self.assertEqual(len(self.q), 0) + # self.q.enqueue(2) + # self.assertEqual(len(self.q), 1) + # self.q.enqueue(4) + # self.assertEqual(len(self.q), 2) + # self.q.enqueue(6) + # self.q.enqueue(8) + # self.q.enqueue(10) + # self.q.enqueue(12) + # self.q.enqueue(14) + # self.q.enqueue(16) + # self.q.enqueue(18) + # self.assertEqual(len(self.q), 9) + + # def test_empty_dequeue(self): + # self.assertIsNone(self.q.dequeue()) + # self.assertEqual(len(self.q), 0) + + def test_dequeue_respects_order(self): + self.q.enqueue(100) + self.q.enqueue(101) + self.q.enqueue(105) + self.assertEqual(self.q.dequeue(), 100) + self.assertEqual(len(self.q), 2) + self.assertEqual(self.q.dequeue(), 101) + self.assertEqual(len(self.q), 1) + self.assertEqual(self.q.dequeue(), 105) + self.assertEqual(len(self.q), 0) + self.assertIsNone(self.q.dequeue()) + self.assertEqual(len(self.q), 0) + +if __name__ == '__main__': + unittest.main() + + + diff --git a/queue/test_queue.py b/queue/test_dll_queue.py similarity index 95% rename from queue/test_queue.py rename to queue/test_dll_queue.py index 4d838826c2..4937912c57 100644 --- a/queue/test_queue.py +++ b/queue/test_dll_queue.py @@ -1,9 +1,9 @@ import unittest -from queue import Queue +from dll_queue import dll_queue class QueueTests(unittest.TestCase): def setUp(self): - self.q = Queue() + self.q = dll_queue() def test_len_returns_0_for_empty_queue(self): self.assertEqual(len(self.q), 0) diff --git a/singly_linked_list/singly_linked_list.py b/singly_linked_list/singly_linked_list.py index e69de29bb2..379d505b1c 100644 --- a/singly_linked_list/singly_linked_list.py +++ b/singly_linked_list/singly_linked_list.py @@ -0,0 +1,91 @@ +class Node: + def __init__(self,value,next=None): + self.value = value + self.next = next + + def get_value(self): + return self.value + + def get_next(self): + return self.next + + def set_next(self, new_next): + self.next = new_next + + +class LinkedList: + def __init__(self): + self.head = None + self.tail = None + + def add_to_head(self, value): + new_node= Node(value) + # create Node from input + if not self.head and not self.tail: + self.head = new_node + self.tail = new_node + else: + new_node.set_next(self.head) + self.head = new_node + + def add_to_tail(self, value): + new_node = Node(value, None) + if not self.head: + # if list is empty + self.head = new_node + self.tail = new_node + else: + # if not empty + self.tail.set_next(new_node) + self.tail = new_node + + def remove_head(self): + if not self.head: + return None + # if head has no next node + if not self.head.get_next(): + head = self.head + # have to change both head and tail since they are both the same item + self.head = None + self.tail = None + return head.get_value() + value = self.head.get_value() + self.head = self.head.get_next() + return value + + def contains(self, value): + if not self.head: + return False + # get a reference to the node we're currently at; update this as we traverse this list + current = self.head + while current: + if current.get_value() == value: + return True + current = current.get_next() + return False + + def get_max(self): + current = self.head + # if there's nothing in the list + if not current: + return None + # if there's only one thing in the list + if not current.get_next(): + return current.get_value() + max = current.get_value() + # mistake: was only comparing the first element and it wasn't moving + # "current" to the next element + while current is not None: + if max < current.get_value(): + max = current.get_value() + current = current.get_next() + return max + + def print_val(self): + #val = self.head.value + curr_node = self.head + + while curr_node is not None: + print (f'##{curr_node.get_value()}\n') + curr_node = curr_node.get_next() + diff --git a/singly_linked_list/test_singly_linked_list.py b/singly_linked_list/test_singly_linked_list.py index f5cdb81aee..10fa139c30 100644 --- a/singly_linked_list/test_singly_linked_list.py +++ b/singly_linked_list/test_singly_linked_list.py @@ -13,11 +13,22 @@ def test_add_to_tail(self): self.assertEqual(self.list.tail.value, 2) self.assertEqual(self.list.head.value, 1) + def test_contains(self): + self.list.add_to_tail(1) + self.list.add_to_tail(2) + self.list.add_to_tail(5) + self.list.add_to_tail(10) + self.assertTrue(self.list.contains(10)) + self.assertTrue(self.list.contains(2)) + self.assertFalse(self.list.contains(1000)) + def test_remove_head(self): self.list.add_to_tail(10) self.list.add_to_tail(20) self.assertEqual(self.list.remove_head(), 10) + self.assertFalse(self.list.contains(10)) self.assertEqual(self.list.remove_head(), 20) + self.assertFalse(self.list.contains(20)) self.list.add_to_tail(10) self.assertEqual(self.list.remove_head(), 10) @@ -25,17 +36,14 @@ def test_remove_head(self): self.assertIsNone(self.list.tail) self.assertIsNone(self.list.remove_head()) - def test_remove_tail(self): - self.list.add_to_tail(30) - self.list.add_to_tail(40) - self.assertEqual(self.list.remove_tail(), 40) - self.assertEqual(self.list.remove_tail(), 30) - - self.list.add_to_tail(100) - self.assertEqual(self.list.remove_tail(), 100) - self.assertIsNone(self.list.head) - self.assertIsNone(self.list.tail) - self.assertIsNone(self.list.remove_tail()) + def test_get_max(self): + self.assertIsNone(self.list.get_max()) + self.list.add_to_tail(100) + self.assertEqual(self.list.get_max(), 100) + self.list.add_to_tail(55) + self.assertEqual(self.list.get_max(), 100) + self.list.add_to_tail(101) + self.assertEqual(self.list.get_max(), 101) if __name__ == '__main__': - unittest.main() + unittest.main() \ No newline at end of file diff --git a/stack/stack.py b/stack/array_stack.py similarity index 69% rename from stack/stack.py rename to stack/array_stack.py index 6e6d660ac7..5ab05d57d7 100644 --- a/stack/stack.py +++ b/stack/array_stack.py @@ -10,16 +10,20 @@ 3. What is the difference between using an array vs. a linked list when implementing a Stack? """ -class Stack: +import array as arr +class arr_Stack: def __init__(self): self.size = 0 - # self.storage = ? + self.storage = arr.array('i',[]) def __len__(self): - pass + return len(self.storage) def push(self, value): - pass + self.storage.insert(0,value) def pop(self): - pass + try: + return self.storage.pop(0) + except(IndexError): + print ('Array is empty!') \ No newline at end of file diff --git a/stack/dll_stack.py b/stack/dll_stack.py new file mode 100644 index 0000000000..a7d848993c --- /dev/null +++ b/stack/dll_stack.py @@ -0,0 +1,36 @@ +""" +A stack is a data structure whose primary purpose is to store and +return elements in Last In First Out order. + +1. Implement the Stack class using an array as the underlying storage structure. + Make sure the Stack tests pass. +2. Re-implement the Stack class, this time using the linked list implementation + as the underlying storage structure. + Make sure the Stack tests pass. +3. What is the difference between using an array vs. a linked list when + implementing a Stack? +""" +import sys +sys.path.append('../doubly_linked_list') +from doubly_linked_list import DoublyLinkedList + +class dll_Stack: + def __init__(self): + self.storage = DoublyLinkedList() + self.size = len(self.storage) + + def __len__(self): + return self.size + + def push(self, value): + self.size +=1 + self.storage.add_to_head(value) + + def pop(self): + if self.size == 0: + print ('DLL is empty!') + return None + else: + pop_val = self.storage.remove_from_head() + self.size -=1 + return pop_val \ No newline at end of file diff --git a/stack/sll_stack.py b/stack/sll_stack.py new file mode 100644 index 0000000000..d438249756 --- /dev/null +++ b/stack/sll_stack.py @@ -0,0 +1,22 @@ +import sys +sys.path.append('../singly_linked_list') +from singly_linked_list import LinkedList + +class sll_stack: + def __init__(self): + self.size = 0 + self.storage = LinkedList() + + def __len__(self): + return self.size + + def push(self,value): + self.storage.add_to_head(value) + self.size +=1 + + def pop(self): + if self.size == 0: + return None + else: + self.size -=1 + return self.storage.remove_head() diff --git a/stack/test_array_stack.py b/stack/test_array_stack.py new file mode 100644 index 0000000000..e7ba4cd6a9 --- /dev/null +++ b/stack/test_array_stack.py @@ -0,0 +1,45 @@ +import unittest +from array_stack import arr_Stack + +class QueueTests(unittest.TestCase): + def setUp(self): + self.stack = arr_Stack() + + def test_len_returns_0_for_empty_stack(self): + self.assertEqual(len(self.stack), 0) + + def test_len_returns_correct_length_after_push(self): + self.assertEqual(len(self.stack), 0) + self.stack.push(2) + self.assertEqual(len(self.stack), 1) + self.stack.push(4) + self.assertEqual(len(self.stack), 2) + self.stack.push(6) + self.stack.push(8) + self.stack.push(10) + self.stack.push(12) + self.stack.push(14) + self.stack.push(16) + self.stack.push(18) + self.assertEqual(len(self.stack), 9) + + def test_empty_pop(self): + self.assertIsNone(self.stack.pop()) + self.assertEqual(len(self.stack), 0) + + def test_pop_respects_order(self): + self.stack.push(100) + self.stack.push(101) + self.stack.push(105) + self.assertEqual(self.stack.pop(), 105) + self.assertEqual(len(self.stack), 2) + self.assertEqual(self.stack.pop(), 101) + self.assertEqual(len(self.stack), 1) + self.assertEqual(self.stack.pop(), 100) + self.assertEqual(len(self.stack), 0) + self.assertIsNone(self.stack.pop()) + self.assertEqual(len(self.stack), 0) + + +if __name__ == '__main__': + unittest.main() diff --git a/stack/test_stack.py b/stack/test_dll_stack.py similarity index 95% rename from stack/test_stack.py rename to stack/test_dll_stack.py index 0d79509d76..1e6953782e 100644 --- a/stack/test_stack.py +++ b/stack/test_dll_stack.py @@ -1,9 +1,9 @@ import unittest -from stack import Stack +from dll_stack import dll_Stack class QueueTests(unittest.TestCase): def setUp(self): - self.stack = Stack() + self.stack = dll_Stack() def test_len_returns_0_for_empty_stack(self): self.assertEqual(len(self.stack), 0) diff --git a/stack/test_sll_stack.py b/stack/test_sll_stack.py new file mode 100644 index 0000000000..6bde53b6ca --- /dev/null +++ b/stack/test_sll_stack.py @@ -0,0 +1,45 @@ +import unittest +from sll_stack import sll_stack + +class QueueTests(unittest.TestCase): + def setUp(self): + self.stack = sll_stack() + + def test_len_returns_0_for_empty_stack(self): + self.assertEqual(len(self.stack), 0) + + def test_len_returns_correct_length_after_push(self): + self.assertEqual(len(self.stack), 0) + self.stack.push(2) + self.assertEqual(len(self.stack), 1) + self.stack.push(4) + self.assertEqual(len(self.stack), 2) + self.stack.push(6) + self.stack.push(8) + self.stack.push(10) + self.stack.push(12) + self.stack.push(14) + self.stack.push(16) + self.stack.push(18) + self.assertEqual(len(self.stack), 9) + + def test_empty_pop(self): + self.assertIsNone(self.stack.pop()) + self.assertEqual(len(self.stack), 0) + + def test_pop_respects_order(self): + self.stack.push(100) + self.stack.push(101) + self.stack.push(105) + self.assertEqual(self.stack.pop(), 105) + self.assertEqual(len(self.stack), 2) + self.assertEqual(self.stack.pop(), 101) + self.assertEqual(len(self.stack), 1) + self.assertEqual(self.stack.pop(), 100) + self.assertEqual(len(self.stack), 0) + self.assertIsNone(self.stack.pop()) + self.assertEqual(len(self.stack), 0) + + +if __name__ == '__main__': + unittest.main()