diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..02e5079 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,89 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def factorial(n) - raise NotImplementedError, "Method not implemented" + if n < 0 + raise ArgumentError + elsif n <= 1 + return 1 + else + n * factorial(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) + def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.empty? || s.length == 1 + return s + else + s[-1] + reverse(s[1..-2]) + s[0] + end end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# s = "bear" +# reverse_in_place!(s) +# s == "raeb" +# "bear" => "reab" => "raeb" +# reverse("bear") calls reverse("ea") calls reverse("") +# reverse_in_place("bear") +# calls r_i_p_helper("bear", 0) +# calls r_i_p_helper("reab", 1) +# calls r_i_p_helper("raeb", 2) -> returns "raeb" +# r_i_p_helper("", 0) s.length = 0, idx = 0 +# ...("a", 0) s.length = 1, idx = 0 +# ...("raeb", 2) s.length = 4, idx = 2 +# ...("butter", 0) => ("rutteb", 1) => ("rettub", 2) ("rettub", 3) => return +def reverse_in_place_helper(s, index) + if index == s.length / 2 + return s + else + temp = s[index] + s[index] = s[s.length - index - 1] + s[s.length - index - 1] = temp + reverse_in_place_helper(s, index + 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) +def reverse_in_place(s) + reverse_in_place_helper(s, 0) +end + +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return 0 + else + return 2 + bunny(n - 1) + end end + # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + raise NotImplementedError, "Method not implemented" +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 3b30725..d633448 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -1,11 +1,10 @@ -require 'minitest/autorun' -require 'minitest/reporters' +require "minitest/autorun" +require "minitest/reporters" require "minitest/skip_dsl" -require_relative '../lib/recursive-methods' +require_relative "../lib/recursive-methods" Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new - describe "factorial" do it "will find the factorial of 0" do # Arrange @@ -26,8 +25,7 @@ answer = factorial(num) # Assert - expect(answer).must_equal 5*4*3*2*1 - + expect(answer).must_equal 5 * 4 * 3 * 2 * 1 end it "will raise an ArgumentError if given a number not >= 0" do @@ -86,14 +84,13 @@ end end - describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" # Act - answer = reverse_inplace(string) + answer = reverse_in_place(string) # Assert expect(answer).must_equal "tac" @@ -104,7 +101,7 @@ string = "a" # Act - answer = reverse_inplace(string) + answer = reverse_in_place(string) # Assert expect(answer).must_equal "a" @@ -115,7 +112,7 @@ string = "" # Act - answer = reverse_inplace(string) + answer = reverse_in_place(string) # Assert expect(answer).must_equal "" @@ -125,7 +122,7 @@ string = "apple" # Act - answer = reverse_inplace(string) + answer = reverse_in_place(string) # Assert expect(answer).must_equal "elppa" @@ -227,40 +224,40 @@ end it "will return true when looking for something in the array" do - # Arrange - item = "a" - array = ["b", "c", "a"] + # Arrange + item = "a" + array = ["b", "c", "a"] - # Act - answer = search(array, item) + # Act + answer = search(array, item) - # Assert - expect(answer).must_equal true + # Assert + expect(answer).must_equal true end it "will return false when looking for something not in the array" do # Arrange item = "x" array = ["b", "c", "a"] - + # Act answer = search(array, item) - + # Assert expect(answer).must_equal false - end - - it "will return true when finding something at the front of the array" do - # Arrange - item = "b" - array = ["b", "c", "a"] - - # Act - answer = search(array, item) - - # Assert - expect(answer).must_equal true - end + end + + it "will return true when finding something at the front of the array" do + # Arrange + item = "b" + array = ["b", "c", "a"] + + # Act + answer = search(array, item) + + # Assert + expect(answer).must_equal true + end end xdescribe "is_palindrome" do @@ -334,7 +331,7 @@ # Assert expect(answer).must_equal 3 end - + it "returns 1 for (0, 0)" do # Arrange num1 = 0 @@ -346,7 +343,7 @@ # Assert expect(answer).must_equal 1 end - + it "returns 1 for (10, 20)" do # Arrange num1 = 10