diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..845fa424 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,38 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# 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) - raise NotImplementedError, "Not implemented yet" + 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 + return stack.empty? end # Time Complexity: ? diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..5036e103 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,63 @@ 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 - return @store.to_s + list = [] + current = @front + + while current != @back + list << @store[current] + current = (current + 1) % @size + end + return list.to_s end end 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 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