From 64b28e3185189db99bae046569151ec35165bc4d Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 2 Mar 2020 19:14:10 -0800 Subject: [PATCH 1/5] Initial set up --- lib/queue.rb | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..fb66b2a5 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,30 +1,41 @@ class Queue - + def initialize - # @store = ... + @store = Array.new(50) + @front = -1 + @back = -1 raise NotImplementedError, "Not yet implemented" end - + def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if @front == -1 && @back == -1 + @front = 0 + @back = 1 + end + if @front == @back + #Decide + end + + @store[@back] = element + @back = (@back + 1) % @store.length end - + def dequeue raise NotImplementedError, "Not yet implemented" end - + def front raise NotImplementedError, "Not yet implemented" end - + def size raise NotImplementedError, "Not yet implemented" end - + def empty? raise NotImplementedError, "Not yet implemented" end - + def to_s return @store.to_s end From 0e89da568418be3eb2087bca40ccfc97f0e24179 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 2 Mar 2020 21:56:21 -0800 Subject: [PATCH 2/5] Wave 1 complete --- lib/queue.rb | 52 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 41 insertions(+), 11 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index fb66b2a5..c146e3de 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -4,16 +4,14 @@ def initialize @store = Array.new(50) @front = -1 @back = -1 - raise NotImplementedError, "Not yet implemented" end def enqueue(element) - if @front == -1 && @back == -1 + if((@front == 0 && @back == @store.length - 1) || (@front == @back + 1)) + raise ArgumentError + elsif @front == -1 && @back == -1 @front = 0 - @back = 1 - end - if @front == @back - #Decide + @back = 0 end @store[@back] = element @@ -21,22 +19,54 @@ def enqueue(element) end def dequeue - raise NotImplementedError, "Not yet implemented" + if @front == -1 + raise ArgumentError + end + + data = @store[@front] + @store[@front] = nil + @front = (@front + 1) % @store.length + + if @front == @back + @front = -1 + @back = -1 + end + + return data end def front - raise NotImplementedError, "Not yet implemented" + # raise NotImplementedError, "Not yet implemented" end def size - raise NotImplementedError, "Not yet implemented" + if self.empty? + raise ArgumentError + end + + return @back - @front end def empty? - raise NotImplementedError, "Not yet implemented" + if @front == -1 && @back == -1 + return true + else + return false + end end def to_s - return @store.to_s + return @store[@front...@back].to_s end end + +q = Queue.new +q.enqueue(0) +# q.enqueue(1) +# q.enqueue(2) +# q.enqueue(3) +p q +# p q.size +p q.dequeue +# p q.size +p q \ No newline at end of file From 844b54cfdb6de9776ab9fe9917c2f0ab7813b9cd Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Mon, 2 Mar 2020 21:58:43 -0800 Subject: [PATCH 3/5] Wave 2 complete --- lib/stack.rb | 26 +++++++++++++++++--------- 1 file changed, 17 insertions(+), 9 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..d1ca0c0c 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,22 +1,30 @@ +require_relative 'linked_list' + 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 nil if self.empty? + element = @store.remove_last() + return element end - + def empty? - raise NotImplementedError, "Not yet implemented" + return @store.empty? end - + def to_s return @store.to_s end end + +# stack = Stack.new +# stack.push(5) +# stack.push(10) +# p stack \ No newline at end of file From a6d3078cfd32c95bc56aade16645fabad2ab1c9a Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Tue, 3 Mar 2020 20:52:40 -0800 Subject: [PATCH 4/5] Wave 3 complete --- lib/problems.rb | 28 +++++++++++++++++++++++++--- lib/queue.rb | 12 ++++++------ 2 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 5085953d..1836833c 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,27 @@ require_relative './stack.rb' -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) - n is chars in string +# Space Complexity: O(1/2n) --> O(n) - n is chars in string def balanced(string) - raise NotImplementedError, "Not implemented yet" + return true if string.empty? + return false if string.length % 2 != 0 + + s = Stack.new + open_bracs = ["(", "[", "{"] + close_bracs = [")", "]", "}"] + + string.each_char do |char| + if open_bracs.include?(char) + s.push(char) + elsif close_bracs.include?(char) + compare = s.pop + if open_bracs.find_index(compare) != close_bracs.find_index(char) + return false + end + end + end + + return true end # Time Complexity: ? @@ -11,3 +29,7 @@ def balanced(string) def evaluate_postfix(postfix_expression) raise NotImplementedError, "Not implemented yet" end + +# p balanced("{[{}{}]}") +# p balanced("{[{}{}]") +# p balanced("{()}[)") \ No newline at end of file diff --git a/lib/queue.rb b/lib/queue.rb index c146e3de..14f81ce4 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -36,7 +36,7 @@ def dequeue end def front - # raise NotImplementedError, "Not yet implemented" + return @store[@front] end def size @@ -60,13 +60,13 @@ def to_s end end -q = Queue.new -q.enqueue(0) +# q = Queue.new +# q.enqueue(0) # q.enqueue(1) # q.enqueue(2) # q.enqueue(3) -p q +# p q # p q.size -p q.dequeue +# p q.dequeue # p q.size -p q \ No newline at end of file +# p q.front \ No newline at end of file From 41a61e6a3433e5e753e23fd4d90138ffe8f05612 Mon Sep 17 00:00:00 2001 From: Brianna Kemp Date: Tue, 3 Mar 2020 21:21:06 -0800 Subject: [PATCH 5/5] Completed optional --- lib/problems.rb | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 1836833c..1c3eb9fc 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -27,9 +27,26 @@ def balanced(string) # Time Complexity: ? # Space Complexity: ? def evaluate_postfix(postfix_expression) - raise NotImplementedError, "Not implemented yet" + operands = ["*", "**", "/", "%", "+", "-"] + s= Stack.new + + postfix_expression.each_char do |char| + if operands.include?(char) + first = s.pop + second = s.pop + result = eval("#{second}#{char}#{first}") + s.push(result) + else + s.push(char) + end + end + + return s.pop end # p balanced("{[{}{}]}") # p balanced("{[{}{}]") -# p balanced("{()}[)") \ No newline at end of file +# p balanced("{()}[)") + +# p evaluate_postfix("35+6*") #48 +# p evaluate_postfix("53+62/*35*+") #39 \ No newline at end of file