From b4a111ca5195f4a2127e7d65e0267743338a7c8a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Thu, 8 Oct 2020 21:59:26 -0700 Subject: [PATCH 1/2] all test passed --- Gemfile | 11 ++++---- Rakefile | 4 +-- lib/max_subarray.rb | 21 +++++++++++----- lib/newman_conway.rb | 40 ++++++++++++++++++++++++++---- test/max_sub_array_test.rb | 21 ++++++++-------- test/newman_conway_test.rb | 51 +++++++++++++++++++------------------- test/test_helper.rb | 9 +++---- 7 files changed, 96 insertions(+), 61 deletions(-) diff --git a/Gemfile b/Gemfile index 04a9dcd..1e2cf3f 100644 --- a/Gemfile +++ b/Gemfile @@ -1,10 +1,9 @@ # frozen_string_literal: true source "https://rubygems.org" -gem 'rake' -gem 'minitest' -gem 'minitest-spec' -gem 'minitest-reporters' +gem "rake" +gem "minitest" +gem "minitest-spec" +gem "minitest-reporters" gem "pry" -gem 'minitest-skip' - +gem "minitest-skip" diff --git a/Rakefile b/Rakefile index 0c2d13f..8aebe3b 100644 --- a/Rakefile +++ b/Rakefile @@ -1,9 +1,9 @@ -require 'rake/testtask' +require "rake/testtask" Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true - t.test_files = FileList['test/*_test.rb'] + t.test_files = FileList["test/*_test.rb"] end task default: :test diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 5204edb..99988bc 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,8 +1,17 @@ - -# Time Complexity: ? -# Space Complexity: ? +# Time Complexity: O(n) +# Space Complexity: O(n) def max_sub_array(nums) - return 0 if nums == nil - - raise NotImplementedError, "Method not implemented yet!" + max_so_far = max_ending_here = nums[0] + for i in 1..nums.length - 1 + # puts nums[i] + puts max_ending_here + max_ending_here = max_ending_here + nums[i] + if max_ending_here < nums[i] + max_ending_here = nums[i] + end + if max_so_far < max_ending_here + max_so_far = max_ending_here + end + end + return max_so_far end diff --git a/lib/newman_conway.rb b/lib/newman_conway.rb index 4c985cd..93fd797 100644 --- a/lib/newman_conway.rb +++ b/lib/newman_conway.rb @@ -1,7 +1,37 @@ +# Time complexity: O(n) +# Space Complexity: O(n) - -# Time complexity: ? -# Space Complexity: ? def newman_conway(num) - raise NotImplementedError, "newman_conway isn't implemented" -end \ No newline at end of file + raise ArgumentError if num <= 0 + return "1" if num == 1 + + cached_array = [1, 1, 1] + + for i in 3..num + current = cached_array[cached_array[i - 1]] + cached_array[i - (cached_array[i - 1])] + cached_array[i] = current + end + cached_array.delete_at(0) + return cached_array.join(" ") +end + + + + + + +# def newman_conway(num) +# raise ArgumentError if num <= 0 +# return "1" if num == 1 + +# cached_array = [0, 1, 1] +# i = 3 +# until i >= num + 1 +# current = cached_array[cached_array[i - 1]] + cached_array[i - (cached_array[i - 1])] + +# cached_array[i] = current +# i += 1 +# end +# cached_array.delete_at(0) +# return cached_array.join(' ') +# end diff --git a/test/max_sub_array_test.rb b/test/max_sub_array_test.rb index 3253cdf..c83ef94 100644 --- a/test/max_sub_array_test.rb +++ b/test/max_sub_array_test.rb @@ -1,9 +1,9 @@ require_relative "test_helper" -xdescribe "max subarray" do +describe "max subarray" do it "will work for [-2,1,-3,4,-1,2,1,-5,4]" do # Arrange - input = [-2,1,-3,4,-1,2,1,-5,4] + input = [-2, 1, -3, 4, -1, 2, 1, -5, 4] # Act answer = max_sub_array(input) @@ -25,7 +25,7 @@ it "will work with a totally negative array with the largest element at the rear" do # Arrange - input = [ -4, -5, -6, -7, -3] + input = [-4, -5, -6, -7, -3] # Act answer = max_sub_array(input) @@ -46,14 +46,14 @@ end it "will return nil for an empty array" do - # Arrange - input = [] + # Arrange + input = [] - # Act - answer = max_sub_array(input) + # Act + answer = max_sub_array(input) - # Assert - expect(answer).must_be_nil + # Assert + expect(answer).must_be_nil end it "will work for [50, -50, 50]" do @@ -66,5 +66,4 @@ # Assert expect(answer).must_equal 50 end - -end \ No newline at end of file +end diff --git a/test/newman_conway_test.rb b/test/newman_conway_test.rb index 537d376..5476d10 100644 --- a/test/newman_conway_test.rb +++ b/test/newman_conway_test.rb @@ -24,31 +24,30 @@ end it "works with base cases" do - # Arrange - input = 0 - - # Act-Assert - expect { - newman_conway(input) - }.must_raise ArgumentError - - - # Arrange - input = 1 - - # Act - answer = newman_conway(input) - - # Assert - expect(answer).must_equal "1" - - # Arrange - input = 2 - - # Act - answer = newman_conway(input) - - # Assert - expect(answer).must_equal "1 1" + # Arrange + input = 0 + + # Act-Assert + expect { + newman_conway(input) + }.must_raise ArgumentError + + # Arrange + input = 1 + + # Act + answer = newman_conway(input) + + # Assert + expect(answer).must_equal "1" + + # Arrange + input = 2 + + # Act + answer = newman_conway(input) + + # Assert + expect(answer).must_equal "1 1" end end diff --git a/test/test_helper.rb b/test/test_helper.rb index 68fa2f2..d672fba 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -1,10 +1,9 @@ - -require 'minitest' -require 'minitest/autorun' -require 'minitest/reporters' +require "minitest" +require "minitest/autorun" +require "minitest/reporters" require "minitest/skip_dsl" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new require_relative "../lib/newman_conway" -require_relative "../lib/max_subarray" \ No newline at end of file +require_relative "../lib/max_subarray" From 401208db426e71142d3bb2b59c76a21b2962eb1e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hannah=20Teki=C3=A9?= Date: Thu, 8 Oct 2020 22:03:49 -0700 Subject: [PATCH 2/2] fixed space complexity --- lib/max_subarray.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/max_subarray.rb b/lib/max_subarray.rb index 99988bc..7dba318 100644 --- a/lib/max_subarray.rb +++ b/lib/max_subarray.rb @@ -1,5 +1,5 @@ # Time Complexity: O(n) -# Space Complexity: O(n) +# Space Complexity: O(1) def max_sub_array(nums) max_so_far = max_ending_here = nums[0] for i in 1..nums.length - 1