From b798493f4954e0e3746a352e5d68d2e50e17c263 Mon Sep 17 00:00:00 2001 From: Bri Latimer <33140749+brilatimer@users.noreply.github.com> Date: Sun, 8 Mar 2020 19:07:13 -0700 Subject: [PATCH 1/3] Update problems.rb --- lib/problems.rb | 45 +++++++++++++++++++++++++++++++++++++++------ 1 file changed, 39 insertions(+), 6 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..208b685d 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,13 +1,46 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def balanced(string) - raise NotImplementedError, "Not implemented yet" + # is it evenly divided by 2? If yes, then return false + # equal number of open parans and close parens + # make a new stack, make balanced = true + # go through each string using each_char + # if open parens, push to stack + # if close parens, check if stack has this match. If yes, pop off match in stack + # if at end, stack is empty, it is balanced. If parens remains, it is not balanced + + if string.length % 2 == 1 + return false + end + + stack = Stack.new + + string.each_char do |char| + if char == '(' + stack.push '(' + elsif char == ')' && stack.pop != '(' + return false + end + + if char == '{' + stack.push '}' + elsif char == '}' && stack.pop != '{' + return false + end + + if char == '[' + stack.push '[' + elsif char == ']' && stack.pop != '[' + return false + end + end + return true end # Time Complexity: ? # Space Complexity: ? -def evaluate_postfix(postfix_expression) - raise NotImplementedError, "Not implemented yet" -end +# def evaluate_postfix(postfix_expression) +# raise NotImplementedError, "Not implemented yet" +# end From cafb4339aec78000a5ee5c80d28385402921c160 Mon Sep 17 00:00:00 2001 From: Bri Latimer <33140749+brilatimer@users.noreply.github.com> Date: Sun, 8 Mar 2020 19:07:41 -0700 Subject: [PATCH 2/3] Update queue.rb --- lib/queue.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..0de46e00 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,16 +1,17 @@ +require 'linked_list.rb' + class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = LinkedList.new end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + @store.add_last(element) end def dequeue - raise NotImplementedError, "Not yet implemented" + return @store.remove_first end def front @@ -22,7 +23,7 @@ def size end def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end def to_s From 02760c1873372e7579d2194e17660df1e52da556 Mon Sep 17 00:00:00 2001 From: Bri Latimer <33140749+brilatimer@users.noreply.github.com> Date: Sun, 8 Mar 2020 19:08:07 -0700 Subject: [PATCH 3/3] Update stack.rb --- lib/stack.rb | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..74c1efa9 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,19 +1,20 @@ +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" + return @store.remove_last end def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end def to_s