From fe82474972f23f8b4249610d904384335ec20e29 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Thu, 5 Sep 2019 16:57:36 -0700 Subject: [PATCH 1/6] Working on dequeue --- lib/queue.rb | 34 ++++++++++++++++++++++++++++------ test/queue_test.rb | 8 ++++---- 2 files changed, 32 insertions(+), 10 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..f707a4e6 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,16 +1,32 @@ +QUEUE_SIZE = 20 + class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(QUEUE_SIZE) + @front = @rear = -1 end + # using circular buffer def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if @front == -1 # Q is empty + @rear = 1 + @front = 0 + @store[@front] = element + + elsif @front == @rear + raise Error, "Queue is full." + + else # not empty + # if we reach the end of the array, rear will once again be pointed to 0 + new_rear = (@rear + 1) % QUEUE_SIZE + @store[@rear] = element + @rear = new_rear + end end def dequeue - raise NotImplementedError, "Not yet implemented" + return element = @store.pop end def front @@ -22,10 +38,16 @@ def size end def empty? - raise NotImplementedError, "Not yet implemented" + return @store[0] == nil && @store[1] == nil end def to_s - return @store.to_s + array = [] + @store.each do |element| + break if element.nil? + + array << element + end + return array.to_s end end diff --git a/test/queue_test.rb b/test/queue_test.rb index 9b616a30..333da5c4 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 From 79f022a12ce1f17e5b727eff195d5325e2b57eee Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Sun, 8 Sep 2019 00:47:01 -0700 Subject: [PATCH 2/6] Added Queue methods --- lib/queue.rb | 30 ++++++++++++++++++++++++++---- lib/stack.rb | 4 ++-- test/queue_test.rb | 6 +++--- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index f707a4e6..078825d8 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -26,19 +26,41 @@ def enqueue(element) end def dequeue - return element = @store.pop + if @front == -1 + return + + elsif @front == @rear + temp_node = @store[@front] + @front = -1 + @rear = -1 + return temp_node + + else + temp_node = @store[@front] + @store[@front] = nil + @front = (@front + 1) % QUEUE_SIZE + return temp_node + end end def front - raise NotImplementedError, "Not yet implemented" + return @store[@front] end def size - raise NotImplementedError, "Not yet implemented" + if @front > @rear + return QUEUE_SIZE - @front + @rear + else + return @rear - @front + end end def empty? - return @store[0] == nil && @store[1] == nil + if @store[@front].nil? + return true + else + return false + end end def to_s diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..808c89ff 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,7 +1,7 @@ class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new + @top = nil end def push(element) diff --git a/test/queue_test.rb b/test/queue_test.rb index 333da5c4..64d5c658 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -42,7 +42,7 @@ end it "removes the right something (LIFO)" do - skip + # skip q = Queue.new q.enqueue(5) q.enqueue(3) @@ -53,7 +53,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) @@ -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) From 4ed87899c7bd21abc91e2a8eb3863e59236a4cc6 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Sun, 8 Sep 2019 17:21:07 -0700 Subject: [PATCH 3/6] Added code for Stack --- lib/stack.rb | 13 ++++++++++--- test/stack_test.rb | 10 +++++----- 2 files changed, 15 insertions(+), 8 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index 808c89ff..c6a8986e 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,3 +1,5 @@ +require_relative './linked_list.rb' + class Stack def initialize @store = LinkedList.new @@ -5,18 +7,23 @@ def initialize end def push(element) - raise NotImplementedError, "Not yet implemented" + @store.add_last(element) end def pop - raise NotImplementedError, "Not yet implemented" + @store.remove_last end def empty? - raise NotImplementedError, "Not yet implemented" + if @store.empty? + return true + else + return false + end end def to_s return @store.to_s end end + 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 33ecd0f366d41411d842412cfa0eb430e50d2e34 Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Tue, 10 Sep 2019 16:06:52 -0700 Subject: [PATCH 4/6] Added code for balanced string --- lib/problems.rb | 45 ++++++++++++++++++++++++++++++++++++++++++- test/problems_test.rb | 8 ++++---- 2 files changed, 48 insertions(+), 5 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 61bcaa5a..73a5edca 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,52 @@ require_relative './stack.rb' def balanced(string) - raise NotImplementedError, "Not implemented yet" + if string == "" + return true + end + + parens = { + "(" => ")", + "[" => "]", + "{" => "}", + } + + stack = Stack.new + string.each_char do |element| + if parens[element] + stack.push(parens[element]) + else + return false if element != stack.pop + end + end + + return stack.empty? end + # PUSH PARENS INTO ARRAYS + # string.each_char do |element| + # if element == "(" || element == "{" || element == "[" + # open.push(element); + # else + # close.push(element); + # end + # end + # if open.length != close.length + # return false + # end + # is_balanced = false + # for index in (0...open.length) + # if open[index] == "(" && close[(close.length - 1) - index] == ")" || + # open[index] == "{" && close[(close.length - 1) - index] == "}" || + # open[index] == "[" && close[(close.length - 1) - index] == "]" + # is_balanced = true + # else + # return is_balanced + # end + # end + # return is_balanced + + def evaluate_postfix(postfix_expression) raise NotImplementedError, "Not implemented yet" end \ No newline at end of file diff --git a/test/problems_test.rb b/test/problems_test.rb index 9d30e1cd..93339628 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 + # skip expect(balanced('(({}))')).must_equal true end it "regards an empty string as balanced" do - skip + # skip expect(balanced('')).must_equal true end it "will return false for an unbalanced set of parens" do - skip + # skip expect(balanced('(()')).must_equal false expect(balanced('(()}')).must_equal false expect(balanced('([]]')).must_equal false end it "also works for {} and []" do - skip + # skip expect(balanced('[]')).must_equal true expect(balanced('{}')).must_equal true end From f6bb9b0c32eba2e10375b58c3bdb535f73917f2c Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Thu, 12 Sep 2019 13:45:09 -0700 Subject: [PATCH 5/6] Added some notes --- lib/problems.rb | 13 +++++++++++++ test/problems_test.rb | 1 + 2 files changed, 14 insertions(+) diff --git a/lib/problems.rb b/lib/problems.rb index 73a5edca..1b6a762d 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -21,8 +21,21 @@ def balanced(string) end return stack.empty? + + # cannot do this either + # if stack.empty? + # return + # end + + #undefined method `length' + # if stack.length == 0 + # return true + # end + end + # open_paren = [] + # close_parens = [] # PUSH PARENS INTO ARRAYS # string.each_char do |element| # if element == "(" || element == "{" || element == "[" diff --git a/test/problems_test.rb b/test/problems_test.rb index 93339628..5ae61fd0 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -21,6 +21,7 @@ expect(balanced('(()')).must_equal false expect(balanced('(()}')).must_equal false expect(balanced('([]]')).must_equal false + expect(balanced('({[}]')).must_equal false end it "also works for {} and []" do From 28195f8344eb193f8f9719ad6290a0697e8818bc Mon Sep 17 00:00:00 2001 From: Faiza Ahsan Date: Thu, 12 Sep 2019 14:22:23 -0700 Subject: [PATCH 6/6] Modified to_s method --- lib/queue.rb | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 078825d8..e7128653 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -26,19 +26,19 @@ def enqueue(element) end def dequeue - if @front == -1 + if @front == -1 # Q is empty return - elsif @front == @rear - temp_node = @store[@front] - @front = -1 - @rear = -1 + elsif @front == @rear # There's only 1 element + temp_node = @store[@front] # "remove" the element + @front = -1 # points to the empty bucket + @rear = -1 # points to the empty bucket return temp_node - else - temp_node = @store[@front] - @store[@front] = nil - @front = (@front + 1) % QUEUE_SIZE + else # there's more than 1 element in the array + temp_node = @store[@front] # "remove" the element + @store[@front] = nil # store is now empty + @front = (@front + 1) % QUEUE_SIZE # wrap front around array return temp_node end end @@ -65,7 +65,8 @@ def empty? def to_s array = [] - @store.each do |element| + @store.each_with_index do |element, i| + next if i < @front break if element.nil? array << element