diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..87809b8c 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,13 +1,63 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - where n is the number of characters in the string +# Space Complexity: O(n) - worse case, all characters are openers and we push them all into the stack. def balanced(string) - raise NotImplementedError, "Not implemented yet" + brackets = { + "(" => ")", + "{" => "}", + "[" => "]" + } + + openers = brackets.keys + closers = brackets.values + + opener_stack = [] + + string.each_char do |char| + if openers.include?(char) + opener_stack.push(char) + end + + if closers.include?(char) + if opener_stack.empty? + return false + else + removed_opener = opener_stack.pop + if char != brackets[removed_opener] + return false + end + end + end + end + + return opener_stack.empty? + end -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - where n is the number of characters in the expression +# Space Complexity: O(n) - We're using a stack to store values. def evaluate_postfix(postfix_expression) - raise NotImplementedError, "Not implemented yet" + stack = [] + postfix_expression.each_char do |element| + if element == element.to_i.to_s + stack.push(element.to_i) + else + b = stack.pop + a = stack.pop + case element + when "+" + stack.push(a + b) + when "-" + stack.push(a - b) + when "*" + stack.push(a * b) + when "/" + stack.push(a / b) + end + end + end + return stack.pop end + + diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..20ea63fd 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,60 @@ class Queue - + MAX_SIZE = 20 def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @list = Array.new(MAX_SIZE) + @front = -1 + @back = -1 end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if (@back == MAX_SIZE - 1 && @front == 0) || (@back == (@front - 1) % (MAX_SIZE - 1)) + raise "queue is full" + elsif @front == -1 # queue is empty? + @front = 0 + @back = 1 + @list[@front] = element + else + @list[@back] = element + @back = (@back + 1) % MAX_SIZE + end + end def dequeue - raise NotImplementedError, "Not yet implemented" + + if @front == -1 + raise "queue is empty" + end + + data = @list[@front] + + if @front == @back # if the queue is now empty + @front = -1 + @back = -1 + else + @front = (@front + 1) % MAX_SIZE + end + + return data end def front - raise NotImplementedError, "Not yet implemented" + return @list[@front] end def size - raise NotImplementedError, "Not yet implemented" + return @front == -1 ? 0 : @back - @front + 1 end def empty? - raise NotImplementedError, "Not yet implemented" + return @front == -1 || @front == @back end def to_s - return @store.to_s + if @front <= @back + return @list.slice(@front...@back).to_s + else + return (@list.slice(@front...MAX_SIZE) + @list.slice(0...@back)).to_s + end end end diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..018504bd 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,18 @@ class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @list = LinkedList.new end def push(element) - raise NotImplementedError, "Not yet implemented" + @list.add_first(element) end def pop - raise NotImplementedError, "Not yet implemented" + @list.remove_first end def empty? - raise NotImplementedError, "Not yet implemented" + @list.empty? end def to_s diff --git a/test/problems_test.rb b/test/problems_test.rb index 046f059e..c0cfc618 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 diff --git a/test/test_helper.rb b/test/test_helper.rb index 426168c8..6dfc6980 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -9,5 +9,4 @@ require_relative "../lib/linked_list.rb" 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