diff --git a/Rakefile b/Rakefile index 0c2d13f..21a8bd9 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require 'rake/testtask' + Rake::TestTask.new do |t| t.libs = ["lib"] t.warning = true diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..0bd8e98 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,47 +1,119 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. - -# Time complexity: ? -# Space complexity: ? +require "pry" +# Time complexity: O(n) +# Space complexity: O(n) def factorial(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError, 'Must provide a number greater than 1' unless n >= 0 + + if n == 1 || n == 0 + return 1 + else + return n * factorial(n - 1) + end + end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + + return s if s.length <= 1 + + last = s[-1] + + rest = s[0...-1] + + return last + reverse(rest) end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def reverse_inplace(string) + return _reverse(string, 0, string.length - 1) end -# Time complexity: ? -# Space complexity: ? +def _reverse(string, i, j) + if j - i <= 0 + return string + else + swap(string, i, j) + return _reverse(string, i += 1, j -=1) + end +end + +def swap(string, i, j) + a = string[i] + string[i] = string[j] + string[j] = a +end + +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return 0 + elsif n == 1 + return 2 + else + 2 + bunny(n - 1) + end end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) + +def nested(string) + if string.length % 2 != 0 + return false + else + recursion(string, 0, string.length - 1) + end end +def recursion(string, start_index, end_index) + if start_index > end_index + return true + elsif string[start_index] == "(" && string[end_index] == ")" + return recursion(string, start_index + 1, end_index - 1) + else + return false + end +end + # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + if array == [] + return false + else + return search_helper(array, value, i) + end end -# Time complexity: ? -# Space complexity: ? +def search_helper(array, value, i) + if array[i] == value + return true + else + recursion(array, value, i += 1) + end +end + +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return is_palindrome(s, 0, s.length - 1 ) end +def is_palindrome(s, start_index, end_index) + if start_index < end_index + return true + elsif s[0] != s[s.length - 1] + return false + else + is_palindrome(s, start_index + 1, end_index - 1) + end +end + # Time complexity: ? # Space complexity: ? def digit_match(n, m) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..f6c7514 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -3,6 +3,8 @@ require "minitest/skip_dsl" require_relative '../lib/recursive-methods' +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + describe "factorial" do it "will find the factorial of 0" do # Arrange @@ -38,7 +40,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -84,7 +86,7 @@ end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +131,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -164,7 +166,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -210,149 +212,149 @@ end end -xdescribe "search" do - it "will return false for empty array" do - # Arrange - item = "a" - array = [] +# xdescribe "search" do +# it "will return false for empty array" do +# # Arrange +# item = "a" +# array = [] - # Act - answer = search(array, item) +# # Act +# answer = search(array, item) - # Assert - expect(answer).must_equal false - end +# # Assert +# expect(answer).must_equal false +# end - it "will return true when looking for something in the array" do - # Arrange - item = "a" - array = ["b", "c", "a"] +# it "will return true when looking for something in the array" do +# # Arrange +# item = "a" +# array = ["b", "c", "a"] - # Act - answer = search(array, item) +# # Act +# answer = search(array, item) - # Assert - expect(answer).must_equal true - end +# # 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"] +# 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) +# # 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"] +# # 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) +# # Act +# answer = search(array, item) - # Assert - expect(answer).must_equal true - end -end - -xdescribe "is_palindrome" do - it "will return true for emptystring" do - # Arrange - string = "" - - # Act - answer = is_palindrome(string) - - # Assert - expect(answer).must_equal true - end - - it "will return true for a palindrome" do - # Arrange - string = "racecar" - - # Act - answer = is_palindrome(string) - - # Assert - expect(answer).must_equal true - end - - it "will return false for a nonpalindrome" do - # Arrange - string = "raecar" - - # Act - answer = is_palindrome(string) - - # Assert - expect(answer).must_equal false - end -end - -xdescribe "digit_match" do - it "returns 4 for 1072503891 and 62530841" do - # Arrange - num1 = 1072503891 - num2 = 62530841 - - # Act - answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 4 - end - - it "returns 0 for nonmatching numbers" do - # Arrange - num1 = 0 - num2 = 62530841 - - # Act - answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 0 - end - - it "returns 3 for 841 and 62530841" do - # Arrange - num1 = 841 - num2 = 62530841 - - # Act - answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 3 - end +# # Assert +# expect(answer).must_equal true +# end +# end + +# xdescribe "is_palindrome" do +# it "will return true for emptystring" do +# # Arrange +# string = "" + +# # Act +# answer = is_palindrome(string) + +# # Assert +# expect(answer).must_equal true +# end + +# it "will return true for a palindrome" do +# # Arrange +# string = "racecar" + +# # Act +# answer = is_palindrome(string) + +# # Assert +# expect(answer).must_equal true +# end + +# it "will return false for a nonpalindrome" do +# # Arrange +# string = "raecar" + +# # Act +# answer = is_palindrome(string) + +# # Assert +# expect(answer).must_equal false +# end +# end + +# xdescribe "digit_match" do +# it "returns 4 for 1072503891 and 62530841" do +# # Arrange +# num1 = 1072503891 +# num2 = 62530841 + +# # Act +# answer = digit_match(num1, num2) + +# # Assert +# expect(answer).must_equal 4 +# end + +# it "returns 0 for nonmatching numbers" do +# # Arrange +# num1 = 0 +# num2 = 62530841 + +# # Act +# answer = digit_match(num1, num2) + +# # Assert +# expect(answer).must_equal 0 +# end + +# it "returns 3 for 841 and 62530841" do +# # Arrange +# num1 = 841 +# num2 = 62530841 + +# # Act +# answer = digit_match(num1, num2) + +# # Assert +# expect(answer).must_equal 3 +# end - it "returns 1 for (0, 0)" do - # Arrange - num1 = 0 - num2 = 0 +# it "returns 1 for (0, 0)" do +# # Arrange +# num1 = 0 +# num2 = 0 - # Act - answer = digit_match(num1, num2) +# # Act +# answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 1 - end +# # Assert +# expect(answer).must_equal 1 +# end - it "returns 1 for (10, 20)" do - # Arrange - num1 = 10 - num2 = 20 - - # Act - answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 1 - end -end +# it "returns 1 for (10, 20)" do +# # Arrange +# num1 = 10 +# num2 = 20 + +# # Act +# answer = digit_match(num1, num2) + +# # Assert +# expect(answer).must_equal 1 +# end +# end