diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..6ba01a5 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,100 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def factorial(n) - raise NotImplementedError, "Method not implemented" + if n < 0 + raise ArgumentError + elsif n <= 1 + 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" -end + reversed_string = "" + if s.length >= 1 + reversed_string = reverse(s[1..-1]) + reversed_string += s[0] + else + return reversed_string + end +end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1) +def reverse_inplace(s, i = 0, j = -1) + n = s.length + if i == n / 2 + return s + else + letter = s[i] + s[i] = s[j] + s[j] = letter + reverse_inplace(s, i + 1, j - 1) + end end -# Time complexity: ? -# Space complexity: ? -def bunny(n) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def bunny(n, ears = 0) + if ears == n + n + return ears + else + ears += n + bunny(n, ears) + end end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def nested(s, low = 0, high = (s.length - 1)) + if s.length.odd? + return false + elsif low < high + if s[low] != "(" && s[high] != ")" + return false + end + return nested(s, low + 1, high - 1) + else + return true + end end -# Time complexity: ? +# Time complexity: O(n) # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" -end + new_array_minus_one = 0..-2 -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if array.empty? + return false + elsif array[-1] == value + return true + else + search(array[new_array_minus_one], value) + end +end + +# Time complexity: O(n) +# Space complexity: O(1) +def is_palindrome(s, i = 0, j = -1) + n = s.length + + if i >= s.length + j + return true + elsif + s[i] != s[j] + return false + elsif s[i] == s[j] + is_palindrome(s, i + 1, j - 1) + end end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..416f05a 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 @@ -164,7 +164,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -210,7 +210,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -260,7 +260,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = ""