diff --git a/.github/PULL_REQUEST_TEMPLATE b/.github/PULL_REQUEST_TEMPLATE index ed27d164..34584fe2 100644 --- a/.github/PULL_REQUEST_TEMPLATE +++ b/.github/PULL_REQUEST_TEMPLATE @@ -4,12 +4,25 @@ Thanks for doing some brain yoga. You are now submitting this assignment! ## Comprehension Questions | Question | Answer | |------------ | --------- | -| What is an ADT? | | -| Describe a Stack | | -| What are the 5 methods in Stack and what does each do? | | -| Describe a Queue | | -| What are the 5 methods in Queue and what does each do? | | -| What is the difference between implementing something and using something? | | +| What is an ADT? | --> Abstract Data type (ADT) is a type (or class) for objects + whose behaviour is defined by a set of value and a set of operations. | + +| Describe a Stack | --> A stack is an abstract data type, (Last in First out) that serves as a collection of elements, + with two principal operations: push, which adds an element to the collection, and + pop, which removes the most recently added element that was not yet removed. | + +| What are the 5 methods in Stack and what does each do? | push : Pushes an element on the top of the stack. + pop : Removes and returns the top element of the stack. + empty : It returns true if nothing is on the top of the stack ( if the stack is empty). Else, returns false. + peek : Returns the element on the top of the stack, but does not remove it. | + +| Describe a Queue | A Queue is an Abstract Data Structure which stores data and allows access in a First-In-First-Out order (FIFO)/last in last out. | + +| What are the 5 methods in Queue and what does each do? | enqueue: add an element to the rear of the queue +dequeue: Remove & return the element from the front +is_empty: returns true if the queue is empty | + +| What is the difference between implementing something and using something? | Implementing something: is to build it and know the how it works. Using something: using existing code | ## OPTIONAL JobSimulation | Question | Answer | diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..0804755c 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -2,30 +2,65 @@ class Queue def initialize # @store = ... - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + @store = Array.new(100) + @front = @back = -1 + # @front = -1 + # @back = -1 end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + if @front == -1 && @back == -1 + @front = 0 + @back = 1 + @store[@front] = element + elsif @front == @back + #DECIDE + raise ArgumentError, "Queue is full!" + else + @store[@back] = element + @back = (@back + 1) % @store.length + end end def dequeue - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + if @front == @back + raise ArgumentError, "Queue is empty!" + end + if element = @store[@front] + @front = (@front + 1) % @store.length + return element + end end def front - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return @store[@front] end def size - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return @store.length end def empty? - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + if @front == @back + return true + else + return false + end end def to_s - return @store.to_s + # return @store.to_s + queue = [] + until @front == @back + queue << @store[@front] + @front = (@front + 1) % @store.length + end + return queue.to_s end end diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..1d97d0f4 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,26 @@ +require_relative "./linked_list" + class Stack def initialize # @store = ... - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end def push(element) - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + @store.add_last(element) end def pop - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return nil if self.empty? + @store.remove_last end def empty? - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return @store.empty? end def to_s diff --git a/test/queue_test.rb b/test/queue_test.rb index 66372e26..bb716a97 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -11,14 +11,12 @@ end it "adds something to an empty Queue" do - skip q = Queue.new q.enqueue(10) expect(q.to_s).must_equal "[10]" end it "adds multiple somethings to a Queue" do - skip q = Queue.new q.enqueue(10) q.enqueue(20) @@ -27,13 +25,11 @@ end it "starts the size of a Queue at 0" do - skip q = Queue.new q.empty?.must_equal true end it "a Queue is empty after removing all the elements" do - skip q = Queue.new q.enqueue(5) q.enqueue(6) @@ -43,7 +39,6 @@ end it "removes something from the Queue" do - skip q = Queue.new q.enqueue(5) removed = q.dequeue @@ -52,7 +47,6 @@ end it "removes the right something (LIFO)" do - skip q = Queue.new q.enqueue(5) q.enqueue(3) @@ -63,7 +57,6 @@ end it "properly adjusts the size with enqueueing and dequeueing" do - skip q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -75,7 +68,6 @@ end it "returns the front element in the Queue" do - skip q = Queue.new q.enqueue(40) q.enqueue(22) diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..2cf9d5d9 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -10,14 +10,12 @@ end it "pushes something onto a empty Stack" do - skip s = Stack.new s.push(10) s.to_s.must_equal "[10]" end it "pushes multiple somethings onto a Stack" do - skip s = Stack.new s.push(10) s.push(20) @@ -26,13 +24,11 @@ end it "starts the stack empty" do - skip s = Stack.new s.empty?.must_equal true end it "removes something from the stack" do - skip s = Stack.new s.push(5) removed = s.pop @@ -41,7 +37,6 @@ end it "removes the right something (LIFO)" do - skip s = Stack.new s.push(5) s.push(3)