From 91ba8f4fcf5fbe0451bdc99504b5ca547a488794 Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 21:36:09 -0700 Subject: [PATCH 1/6] stacks passing tests --- lib/stack.rb | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..6d1c1f72 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,24 @@ class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end - def push(element) - raise NotImplementedError, "Not yet implemented" + # Adds the value to the top of the stack + def push(value) + @store.add_last(value) end + # Removes and returns an element from the top of the stack def pop - raise NotImplementedError, "Not yet implemented" + return nil if @store.empty? + + @store.remove_last end + # returns true if the stack is empty and false otherwise def empty? - raise NotImplementedError, "Not yet implemented" + return true if @store.empty? + return false # if not empty end def to_s From ad5819b51e7d2d825ee5820fa157a32cbe7d880e Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 22:29:39 -0700 Subject: [PATCH 2/6] need to fix the to_s so tests pass for queues --- lib/queue.rb | 49 +++++++++++++++++++++++++++++++++++++------------ 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..1f98872d 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,28 +1,53 @@ class Queue +# Using a circular buffer with an internal array starting at 20 elements, implement a Queue + def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(20) + @size = 20 + @front = -1 + @back = -1 end - def enqueue(element) - raise NotImplementedError, "Not yet implemented" + # Adds the value to the back of the queue. + def enqueue(value) + if @front == -1 + @front = 0 + @back = 1 + @store[@front] = value + elsif @store.empty? + raise ArgumentError, "Queue is full" + else + @store[@back] = value + @back = (@back + 1) % @size + end end + # removes and returns a value from the front of the queue def dequeue - raise NotImplementedError, "Not yet implemented" + if @front == -1 + raise ArgumentError, "Queue is empty" + else + data = @store[@front] + @front = (@front + 1) % @size + if @store.empty? + @front = back = -1 + end + return data + end end - def front - raise NotImplementedError, "Not yet implemented" - end + # def front + # raise NotImplementedError, "Not yet implemented" + # end - def size - raise NotImplementedError, "Not yet implemented" - end + # def size + # raise NotImplementedError, "Not yet implemented" + # end + # returns true if the queue is empty and false otherwise def empty? - raise NotImplementedError, "Not yet implemented" + return @front == @back end def to_s From dd20d132f6136b3c9fc63fdc7786ab66dd17a0a0 Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 22:34:10 -0700 Subject: [PATCH 3/6] passing queue tests --- lib/queue.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/lib/queue.rb b/lib/queue.rb index 1f98872d..5036e103 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -51,6 +51,13 @@ def empty? end def to_s - return @store.to_s + list = [] + current = @front + + while current != @back + list << @store[current] + current = (current + 1) % @size + end + return list.to_s end end From 9e49f58e8e006d2cb03467e02d3728d9c5c1ec36 Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 22:50:47 -0700 Subject: [PATCH 4/6] added problem tests --- test/problems_test.rb | 4 ++-- test/test_helper.rb | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/test/problems_test.rb b/test/problems_test.rb index 046f059e..694262b8 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -2,7 +2,7 @@ Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new -xdescribe "Test wave 3 problems" do +describe "Test wave 3 problems" do describe "balanced" do it "Given balanced strings it should return true" do @@ -33,7 +33,7 @@ end end - describe "postfix" do + xdescribe "postfix" do it "can add a 2 numbers together" do expect(evaluate_postfix("34+")).must_equal 7 diff --git a/test/test_helper.rb b/test/test_helper.rb index 426168c8..f9340080 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -10,4 +10,4 @@ require_relative "../lib/queue.rb" require_relative "../lib/stack.rb" # Extra exercises -# require_relative "../lib/problems.rb" \ No newline at end of file +require_relative "../lib/problems.rb" \ No newline at end of file From 426792a7027c06162a67af6981fa5a505ddd5833 Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 22:51:01 -0700 Subject: [PATCH 5/6] added brackets psuedo code --- lib/problems.rb | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..fae51529 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,21 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) + +# Given a string containing opening and closing braces, check if it represents a balanced expression or not. def balanced(string) - raise NotImplementedError, "Not implemented yet" + stack = Stack.new + string.each_char do |char| + # if character is an opening bracket + # push to stack + # if not + # pop the top character from the stack + # if the current character is a closing bracket + # return false if the top stack character is not a matching open + + # return true if the stack is empty, false if not + end end # Time Complexity: ? From d7e29ce61f87d97a2d17a70ea14e995fee2c276c Mon Sep 17 00:00:00 2001 From: thenora Date: Tue, 8 Sep 2020 23:01:33 -0700 Subject: [PATCH 6/6] passing tests --- lib/problems.rb | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index fae51529..845fa424 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,21 +1,38 @@ require_relative './stack.rb' -# Time Complexity: O(n) -# Space Complexity: O(n) +# Time Complexity: O(n) - length of the string +# Space Complexity: O(n) - # of opening brackets # Given a string containing opening and closing braces, check if it represents a balanced expression or not. def balanced(string) stack = Stack.new + opening = ["(", "[", "{"] + string.each_char do |char| # if character is an opening bracket + if opening.include?(char) # push to stack + stack.push(char) # if not + else # pop the top character from the stack + top_stack = stack.pop + if char == ')' + return false if top_stack != '(' + elsif char == ']' + return false if top_stack != '[' + elsif char == '}' + return false if top_stack != '{' # if the current character is a closing bracket # return false if the top stack character is not a matching open + end + end + + + end # return true if the stack is empty, false if not - end + return stack.empty? end # Time Complexity: ?