From c9e0943b390add2a382d126ecddd0080e54571bc Mon Sep 17 00:00:00 2001 From: Mariya Burrows Date: Mon, 2 Mar 2020 22:19:39 -0800 Subject: [PATCH] completed waves 1 && 2 --- lib/queue.rb | 54 ++++++++++++++++++++++++++++++++++------------ lib/stack.rb | 19 +++++++++------- test/queue_test.rb | 27 +++++++++-------------- test/stack_test.rb | 15 +++++-------- 4 files changed, 66 insertions(+), 49 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..085fac44 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,57 @@ class Queue - + def initialize # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(10) + @front = -1 + @back = -1 end - + def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if @front == -1 && @back == -1 + @front = 0 + @back = 0 + end + + if @front == @back && @store.compact.length > 0 + front_array = @store.slice(0, @front) + back_array = @store.slice(@back, @store.length - 1) + @store = front_array + Array.new(10) + back_array + @front = (@front + 10) % @store.length + end + + @store[@back] = element + @back = (@back + 1) % @store.length end - + def dequeue - raise NotImplementedError, "Not yet implemented" + removed = @store[@front] + @store[@front] = nil + @front = (@front + 1) % @store.length + return removed end - + def front - raise NotImplementedError, "Not yet implemented" + return @store[@front] end - + def size - raise NotImplementedError, "Not yet implemented" + return @store.length end - + def empty? - raise NotImplementedError, "Not yet implemented" + if @front == @back + return true + else + return false + end end - + def to_s - return @store.to_s + if @front >= @back + return (@store[@front..-1] + @store[0...@back]).to_s + else + return @store[@front...@back].to_s + end end end diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..4ff1a454 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,21 +1,24 @@ +require_relative './linked_list' + class Stack def initialize # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end - + def push(element) - raise NotImplementedError, "Not yet implemented" + # add to end of linked list + @store.add_last(element) end - + def pop - raise NotImplementedError, "Not yet implemented" + @store.remove_last end - + def empty? - raise NotImplementedError, "Not yet implemented" + @store.empty? end - + def to_s return @store.to_s end diff --git a/test/queue_test.rb b/test/queue_test.rb index 8a7dcd9c..5357c565 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -9,40 +9,35 @@ q = Queue.new q.class.must_equal Queue end - + it "adds something to an empty Queue" do - skip q = Queue.new q.enqueue(10) 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) q.enqueue(30) q.to_s.must_equal "[10, 20, 30]" end - + it "starts the size of a Queue at 0" do - skip q = Queue.new q.empty?.must_equal true end - + it "removes something from the Queue" do - skip q = Queue.new q.enqueue(5) removed = q.dequeue removed.must_equal 5 q.empty?.must_equal true end - + it "removes the right something (LIFO)" do - skip q = Queue.new q.enqueue(5) q.enqueue(3) @@ -51,9 +46,8 @@ removed.must_equal 5 q.to_s.must_equal "[3, 7]" end - + it "properly adjusts the size with enqueueing and dequeueing" do - skip q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -63,9 +57,8 @@ q.dequeue q.empty?.must_equal true end - + it "returns the front element in the Queue" do - skip q = Queue.new q.enqueue(40) q.enqueue(22) @@ -73,6 +66,7 @@ q.dequeue expect(q.dequeue).must_equal 22 end + it "works for a large Queue" do q = Queue.new q.enqueue(10) @@ -92,7 +86,6 @@ q.enqueue(130) q.enqueue(140) q.enqueue(150) - q.enqueue(150) q.enqueue(160) q.enqueue(170) q.enqueue(180) @@ -100,7 +93,7 @@ q.enqueue(200) q.enqueue(210) q.dequeue - - expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210, 220, 230, 240]') + + expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200, 210]') end end diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..621c8d09 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -8,40 +8,35 @@ s = Stack.new s.class.must_equal Stack 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) s.push(30) s.to_s.must_equal "[10, 20, 30]" 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 removed.must_equal 5 s.empty?.must_equal true end - + it "removes the right something (LIFO)" do - skip s = Stack.new s.push(5) s.push(3)