From 0ce544f0d507779019d3a61531bc6982fdaf96a3 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 1 Mar 2020 12:48:20 -0800 Subject: [PATCH 1/4] Wave 1 - Implement a Stack - tests pass --- lib/stack.rb | 22 +++++++++++++--------- test/stack_test.rb | 33 ++++++++++++++------------------- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..e3879c57 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,21 +1,25 @@ +require_relative './linked_list.rb' + class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end - + def push(element) - raise NotImplementedError, "Not yet implemented" + @store.add_last(element) end - + def pop - raise NotImplementedError, "Not yet implemented" + return nil if self.empty? + element = @store.remove_last + + return element end - + def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end - + def to_s return @store.to_s end diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..7ef0035f 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -6,48 +6,43 @@ describe "Test Stack Implementation" do it "creates a Stack" do s = Stack.new - s.class.must_equal Stack + _(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]" + _(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]" + _(s.to_s).must_equal "[10, 20, 30]" end - + it "starts the stack empty" do - skip s = Stack.new - s.empty?.must_equal true + _(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 + _(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) s.push(7) removed = s.pop - removed.must_equal 7 - s.to_s.must_equal "[5, 3]" + _(removed).must_equal 7 + _(s.to_s).must_equal "[5, 3]" end -end \ No newline at end of file +end From edac71f18e048ef15dd42774201ea768e1b7a131 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 1 Mar 2020 17:21:56 -0800 Subject: [PATCH 2/4] Wave 2 Implement a Queue --- test/queue_test.rb | 50 +++++++++++++++++++++------------------------- 1 file changed, 23 insertions(+), 27 deletions(-) diff --git a/test/queue_test.rb b/test/queue_test.rb index 8a7dcd9c..d608b1d8 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -7,65 +7,58 @@ describe "Test Queue Implementation" do it "creates a Queue" do q = Queue.new - q.class.must_equal Queue + _(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]" + _(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]" + _(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 + _(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 + _(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) q.enqueue(7) removed = q.dequeue - removed.must_equal 5 - q.to_s.must_equal "[3, 7]" + _(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.empty?).must_equal true q.enqueue(-1) q.enqueue(-60) - q.empty?.must_equal false + _(q.empty?).must_equal false q.dequeue q.dequeue - q.empty?.must_equal true + _(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,15 +86,17 @@ q.enqueue(130) q.enqueue(140) q.enqueue(150) - q.enqueue(150) q.enqueue(160) q.enqueue(170) q.enqueue(180) q.enqueue(190) q.enqueue(200) q.enqueue(210) + q.enqueue(220) + q.enqueue(230) + q.enqueue(240) 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]') end end From deeb5aff8f3ca6759a1dbb79460ae19ae622aca4 Mon Sep 17 00:00:00 2001 From: Sam Date: Sun, 1 Mar 2020 17:23:28 -0800 Subject: [PATCH 3/4] Wave 2 Implement a Queue --- lib/queue.rb | 56 ++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 39 insertions(+), 17 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..063dadb3 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,53 @@ class Queue - def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(50) + @front = @back = -1 end - + def enqueue(element) - raise NotImplementedError, "Not yet implemented" + # queue is empty + if @front == -1 && @back == -1 + @front = 0 + @back = 1 + end + + # queue is full + if @front == @back + raise ArgumentError, "Queue is full" + end + + @store[@back] = element + @back = (@back + 1) % @store.length + + if @store[@front].nil? + @front = (@front + 1) % @store.length + end end - + def dequeue - raise NotImplementedError, "Not yet implemented" + if @front == -1 + raise ArgumentError, "Queue is empty" + end + + 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[@front...@back].length end - + def empty? - raise NotImplementedError, "Not yet implemented" - end - + return @front == -1 + end + def to_s - return @store.to_s + return @store[@front...@back].to_s end -end +end From 29d8f011232fc360b0301620aecd6055f34b37b9 Mon Sep 17 00:00:00 2001 From: Sam Date: Thu, 5 Mar 2020 14:48:14 -0800 Subject: [PATCH 4/4] Wave 2 Implement a Queue - all tests pass --- lib/queue.rb | 36 ++++++++++++++++++++++-------------- 1 file changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 063dadb3..df4dcd02 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,6 +1,6 @@ class Queue def initialize - @store = Array.new(50) + @store = Array.new(30) @front = @back = -1 end @@ -9,18 +9,15 @@ def enqueue(element) if @front == -1 && @back == -1 @front = 0 @back = 1 - end - - # queue is full - if @front == @back + @store[@front] = element + + elsif @front == @back + #queue is full raise ArgumentError, "Queue is full" - end - - @store[@back] = element - @back = (@back + 1) % @store.length - - if @store[@front].nil? - @front = (@front + 1) % @store.length + + else + @store[@back] = element + @back = (@back + 1) % @store.length end end @@ -30,8 +27,12 @@ def dequeue end removed = @store[@front] - @store[@front] = nil @front = (@front + 1) % @store.length + + if @front == @back + @front = @back = -1 + end + return removed end @@ -48,6 +49,13 @@ def empty? end def to_s - return @store[@front...@back].to_s + list = [] + current = @front + + while current != @back + list << @store[current] + current = (current + 1) % @store.length + end + return list.to_s end end