diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..9071afc 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,85 @@ # 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" + if n < 0 + raise ArgumentError + end + return 1 if n == 0 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) +# I think this is reversing the string in place? def reverse(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + return reverse(s[1..-1]) + s[0] end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + return reverse_inplace(s[1..-1]) + s[0] end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return n if n == 0 + return 2 + bunny(n-1) end # Time complexity: ? # Space complexity: ? +# NOT PASSING TESTS def nested(s) - raise NotImplementedError, "Method not implemented" + return true if s.length == 0 + # need to check if s = even number of paren-pairs() + # return true if s.length % 2 == 0 end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def search(array, value, i = 0) + return false if i >= array.length + return true if array[i] == value + return search(array, value, i + 1) end -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +# ONLY PASSING 2 TESTS +def is_palindrome(s, l = 0, r = s.length-1 ) + if s[l] == s[r] + return true + else + return false + end + + while l < s.length + return is_palindrome(s, l + 1, r - 1) + end end # Time complexity: ? # Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" +# NOT PASSING TESTS +def digit_match(n, m, i = -1) +# i want to check each index from the end and keep track of matching values + n = n.to_s + m = m.to_s + tracker = 0 + + if n[i] == m[i] + tracker += 1 + else + tracker += 0 + end + + return tracker if i < -(n.length) + + return digit_match(n, m, i - 1) end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..36e73c4 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -38,7 +38,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -84,7 +84,7 @@ end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +129,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0