From 12e57cbecfdcf2b1f7a60bf62a70addcafa7fe23 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Mon, 9 Sep 2019 22:32:56 -0700 Subject: [PATCH 1/4] passed stack tests with linked list --- lib/stack.rb | 16 +++++++++++----- test/stack_test.rb | 10 +++++----- 2 files changed, 16 insertions(+), 10 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..d3af5399 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,25 @@ +require "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" + removed = @store.remove_last + return removed end def empty? - raise NotImplementedError, "Not yet implemented" + if @store.length > 0 + return false + else + return true + end end def to_s diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..a790a3d0 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 + 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 +26,13 @@ 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 +41,7 @@ end it "removes the right something (LIFO)" do - skip + s = Stack.new s.push(5) s.push(3) From 8b4368182c350499566779a5b4bc8c8bd485aa61 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Mon, 9 Sep 2019 22:43:20 -0700 Subject: [PATCH 2/4] queue tests passed --- lib/queue.rb | 19 ++++++++++++------- test/queue_test.rb | 13 ++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..59d7b88f 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,28 +1,33 @@ class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = [] end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + @store.push(element) end def dequeue - raise NotImplementedError, "Not yet implemented" + removed = @store[0] + @store = @store[1..-1] + return removed end def front - raise NotImplementedError, "Not yet implemented" + return @store[0] end def size - raise NotImplementedError, "Not yet implemented" + return @store.length end def empty? - raise NotImplementedError, "Not yet implemented" + if @store.length > 0 + return false + else + return true + end end def to_s diff --git a/test/queue_test.rb b/test/queue_test.rb index 9b616a30..91769ea9 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -11,14 +11,13 @@ 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) @@ -27,13 +26,13 @@ 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 @@ -42,7 +41,7 @@ end it "removes the right something (LIFO)" do - skip + q = Queue.new q.enqueue(5) q.enqueue(3) @@ -53,7 +52,7 @@ end it "properly adjusts the size with enqueueing and dequeueing" do - skip + q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -65,7 +64,7 @@ end it "returns the front element in the Queue" do - skip + q = Queue.new q.enqueue(40) q.enqueue(22) From 99cf80b318e14a82e009cdc2c4bbff47b650bc8f Mon Sep 17 00:00:00 2001 From: Nidhi Date: Mon, 9 Sep 2019 23:38:19 -0700 Subject: [PATCH 3/4] balanced string tests passed --- lib/problems.rb | 21 ++++++++++++++++++++- test/problems_test.rb | 8 ++++---- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 61bcaa5a..8fea81d0 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,7 +1,26 @@ require_relative './stack.rb' def balanced(string) - raise NotImplementedError, "Not implemented yet" + pairs = {")" => "(", "}" => "{", "]"=>"["} + character_stack = Stack.new + i = 0 + while i < string.length + if pairs.has_value?(string[i]) == true + character_stack.push(string[i]) + else + if character_stack.pop != pairs[string[i]] + return false + end + end + i+=1 + end + + if character_stack.empty? == true + return true + else + return false + end + end def evaluate_postfix(postfix_expression) diff --git a/test/problems_test.rb b/test/problems_test.rb index 9d30e1cd..974b0458 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -7,24 +7,24 @@ describe "Test wave 3 problems" do describe "balanced" do it "Given balanced strings it should return true" do - skip + expect(balanced('(({}))')).must_equal true end it "regards an empty string as balanced" do - skip + expect(balanced('')).must_equal true end it "will return false for an unbalanced set of parens" do - skip + expect(balanced('(()')).must_equal false expect(balanced('(()}')).must_equal false expect(balanced('([]]')).must_equal false end it "also works for {} and []" do - skip + expect(balanced('[]')).must_equal true expect(balanced('{}')).must_equal true end From 6d064fe2a45b14e54a1f3862216353b1521d9d33 Mon Sep 17 00:00:00 2001 From: Nidhi Date: Thu, 12 Sep 2019 13:36:32 -0700 Subject: [PATCH 4/4] added circular buffer to queue --- lib/queue.rb | 52 ++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 42 insertions(+), 10 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 59d7b88f..c824deaf 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,21 +1,44 @@ class Queue def initialize + @BUFFER_SIZE = 10 @store = [] + @front = -1 + @rear = -1 end def enqueue(element) - @store.push(element) + if @front == 0 && @rear == @BUFFER_SIZE - 1 + raise Error, "Queue is Full" + elsif @front == -1 + @front = 0 + @rear = 0 + @store[@rear] = element + elsif @rear == @store.length - 1 && @front != 0 + @rear = 0 + @store[@rear] = element + else + @rear += 1 + @store[@rear] = element + end end def dequeue - removed = @store[0] - @store = @store[1..-1] - return removed + removed_data = @store[@front] + @store[@front] = nil + if (@front == @rear) + @front = -1 + @rear = -1 + elsif @front === @BUFFER_SIZE - 1 + @front = 0 + else + @front += 1 + end + return removed_data end def front - return @store[0] + return @store[@front] end def size @@ -23,14 +46,23 @@ def size end def empty? - if @store.length > 0 - return false - else + if @front == -1 return true end + return false end def to_s - return @store.to_s + display_store = [] + if @front <= @rear + display_store = @store[@front...@rear+1] + else + first_half = @store[0...@rear] + second_half = @store[@front..@store.length+1] + display_store.push(first_half) + display_store.push(second_half) + end + return display_store.to_s end -end + +end \ No newline at end of file