From c7006721f7a26e1420d127a11dabc0ada83de269 Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Sun, 8 Sep 2019 09:21:34 -0700 Subject: [PATCH 1/3] Implement stack methods --- lib/stack.rb | 17 +++++++++++------ test/stack_test.rb | 13 ++++--------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/stack.rb b/lib/stack.rb index cfc6ef0f..fdee7f2c 100644 --- a/lib/stack.rb +++ b/lib/stack.rb @@ -1,22 +1,27 @@ +require "linked_list" + class Stack def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @list = LinkedList.new end def push(element) - raise NotImplementedError, "Not yet implemented" + @list.add_last(element) end def pop - raise NotImplementedError, "Not yet implemented" + return nil if self.empty? + + element = @list.remove_last + + return element end def empty? - raise NotImplementedError, "Not yet implemented" + return @list.empty? end def to_s - return @store.to_s + return @list.to_s end end diff --git a/test/stack_test.rb b/test/stack_test.rb index df5046c8..3511e6a8 100644 --- a/test/stack_test.rb +++ b/test/stack_test.rb @@ -1,6 +1,6 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/stack' +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/stack" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "Test Stack Implementation" do @@ -10,14 +10,12 @@ end it "pushes something onto a empty Stack" do - skip s = Stack.new s.push(10) s.to_s.must_equal "[10]" end it "pushes multiple somethings onto a Stack" do - skip s = Stack.new s.push(10) s.push(20) @@ -26,13 +24,11 @@ end it "starts the stack empty" do - skip s = Stack.new s.empty?.must_equal true end it "removes something from the stack" do - skip s = Stack.new s.push(5) removed = s.pop @@ -41,7 +37,6 @@ end it "removes the right something (LIFO)" do - skip s = Stack.new s.push(5) s.push(3) @@ -50,4 +45,4 @@ removed.must_equal 7 s.to_s.must_equal "[5, 3]" end -end \ No newline at end of file +end From a89a4a3fa4deb6495f5735ed7df934e14ba41af1 Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Sun, 8 Sep 2019 15:01:10 -0700 Subject: [PATCH 2/3] Implement queue methods --- lib/queue.rb | 47 +++++++++++++++++++++++++++++++++++++--------- test/queue_test.rb | 15 ++++----------- 2 files changed, 42 insertions(+), 20 deletions(-) diff --git a/lib/queue.rb b/lib/queue.rb index 828217c6..96d04335 100644 --- a/lib/queue.rb +++ b/lib/queue.rb @@ -1,31 +1,60 @@ -class Queue +QUEUE_SIZE = 20 +class Queue def initialize - # @store = ... - raise NotImplementedError, "Not yet implemented" + @store = Array.new(QUEUE_SIZE) + @front = @rear = -1 end def enqueue(element) - raise NotImplementedError, "Not yet implemented" + if @front == -1 + @rear = 1 + @front = 0 + @store[@front] = element + elsif @front == @rear + raise Error, "Queue full" + else + new_rear = (@rear + 1) % QUEUE_SIZE + @store[@rear] = element + @rear = new_rear + end end def dequeue - raise NotImplementedError, "Not yet implemented" + removed = @store[@front] + if @front + 1 == @rear + @front = -1 + @rear = -1 + else + @store[@front] = nil + @front = (@front + 1) % QUEUE_SIZE + end + return removed end def front - raise NotImplementedError, "Not yet implemented" + if @store.empty? + return -1 + else + return @store[@front] + end end def size - raise NotImplementedError, "Not yet implemented" + return @store.size end def empty? - raise NotImplementedError, "Not yet implemented" + return @front == -1 end def to_s - return @store.to_s + store_string = [] + @store.each do |num| + if num != nil + store_string << num + end + end + return store_string.to_s end end diff --git a/test/queue_test.rb b/test/queue_test.rb index 9b616a30..453d959d 100644 --- a/test/queue_test.rb +++ b/test/queue_test.rb @@ -1,6 +1,6 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/queue' +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/queue" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new @@ -11,14 +11,12 @@ end it "adds something to an empty Queue" do - skip q = Queue.new q.enqueue(10) q.to_s.must_equal "[10]" end it "adds multiple somethings to a Queue" do - skip q = Queue.new q.enqueue(10) q.enqueue(20) @@ -27,13 +25,11 @@ end it "starts the size of a Queue at 0" do - skip q = Queue.new q.empty?.must_equal true end it "removes something from the Queue" do - skip q = Queue.new q.enqueue(5) removed = q.dequeue @@ -42,7 +38,6 @@ end it "removes the right something (LIFO)" do - skip q = Queue.new q.enqueue(5) q.enqueue(3) @@ -53,7 +48,6 @@ end it "properly adjusts the size with enqueueing and dequeueing" do - skip q = Queue.new q.empty?.must_equal true q.enqueue(-1) @@ -65,7 +59,6 @@ end it "returns the front element in the Queue" do - skip q = Queue.new q.enqueue(40) q.enqueue(22) @@ -73,4 +66,4 @@ q.dequeue expect(q.dequeue).must_equal 22 end -end \ No newline at end of file +end From 4734b19eebb787d947640f3356cf86df0f948249 Mon Sep 17 00:00:00 2001 From: Kelly Downes Date: Sun, 8 Sep 2019 16:26:59 -0700 Subject: [PATCH 3/3] Complete balanced string method --- lib/problems.rb | 29 ++++++++++++++++++++++++++--- test/problems_test.rb | 26 +++++++++++--------------- 2 files changed, 37 insertions(+), 18 deletions(-) diff --git a/lib/problems.rb b/lib/problems.rb index 61bcaa5a..85cd8060 100644 --- a/lib/problems.rb +++ b/lib/problems.rb @@ -1,9 +1,32 @@ -require_relative './stack.rb' +require_relative "./stack.rb" def balanced(string) - raise NotImplementedError, "Not implemented yet" + return true if string == "" + + stack = Stack.new + + string.each_char do |bracket| + case bracket + when "(", "{", "[" + stack.push(bracket) + when ")" + if stack.pop != "(" + return false + end + when "}" + if stack.pop != "{" + return false + end + when "]" + if stack.pop != "[" + return false + end + end + end + + return stack.empty? end def evaluate_postfix(postfix_expression) raise NotImplementedError, "Not implemented yet" -end \ No newline at end of file +end diff --git a/test/problems_test.rb b/test/problems_test.rb index 9d30e1cd..c0657a6b 100644 --- a/test/problems_test.rb +++ b/test/problems_test.rb @@ -1,32 +1,28 @@ -require 'minitest/autorun' -require 'minitest/reporters' -require_relative '../lib/problems' +require "minitest/autorun" +require "minitest/reporters" +require_relative "../lib/problems" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new describe "Test wave 3 problems" do describe "balanced" do it "Given balanced strings it should return true" do - skip - expect(balanced('(({}))')).must_equal true + expect(balanced("(({}))")).must_equal true end it "regards an empty string as balanced" do - skip - expect(balanced('')).must_equal true + expect(balanced("")).must_equal true end it "will return false for an unbalanced set of parens" do - skip - expect(balanced('(()')).must_equal false - expect(balanced('(()}')).must_equal false - expect(balanced('([]]')).must_equal false + expect(balanced("(()")).must_equal false + expect(balanced("(()}")).must_equal false + expect(balanced("([]]")).must_equal false end it "also works for {} and []" do - skip - expect(balanced('[]')).must_equal true - expect(balanced('{}')).must_equal true + expect(balanced("[]")).must_equal true + expect(balanced("{}")).must_equal true end end @@ -49,4 +45,4 @@ expect(evaluate_postfix("62/5+")).must_equal 8 end end -end \ No newline at end of file +end