diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..e0b8e3f 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,27 +1,53 @@ # 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 + return 1 + else + return n * (factorial(n - 1)) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n^2) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + end + reversed_s = reverse(s[1..-1]) + reversed_s += s[0] + reversed_s end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + i = 0 + k = s.length - 1 + + return helper(s, i, j) + end + + def helper(s, i, k) + if k <= i + return s + end + + temp = s[i] + s[i] = s[k] + s[k] = temp + return helper(s, i + 1, k - 1) end -# Time complexity: ? -# Space complexity: ? +# 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: ?