diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..a096347 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,94 @@ # 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 || n == 1 + return 1 + elsif n < 0 + raise ArgumentError.new("Parameter must be a positive number") + else + return n * factorial(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(2^n) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + else + return s[-1] + reverse(s[0...-1]) + end end + # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" 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 n + else + return 2 + bunny(n-1) + end end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n) +# Space complexity: O(n^2) def nested(s) - raise NotImplementedError, "Method not implemented" + if s == "" + return true + end + if s[0] != "(" || s[-1] != ")" + return false + end + return nested(s[1...-1]) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n^2) def search(array, value) - raise NotImplementedError, "Method not implemented" + if array.length == 0 + return false + end + if value == array[0] + return true + else + return search(array[1..-1], value) + end end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n) +# Space complexity: O(2^n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s == "" + return true + end + if s[0] != s[-1] + return false + end + return is_palindrome(s[1...-1]) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + if n == 0 || m == 0 + return 0 + end + if n%10 == m%10 + value = 1 + else + value = 0 + end + return value + digit_match(n/10, m/10) end \ No newline at end of file diff --git a/specs/recursion_writing_spec.rb b/specs/recursion_writing_spec.rb index d8fb1ed..5ecb2db 100644 --- a/specs/recursion_writing_spec.rb +++ b/specs/recursion_writing_spec.rb @@ -27,6 +27,7 @@ end + it "will raise an ArgumentError if given a number not >= 0" do # Arrange num = -1 @@ -38,7 +39,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +130,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -164,7 +165,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -210,7 +211,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -260,7 +261,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" @@ -295,7 +296,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891