diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..673b77b 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,87 @@ # 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" + raise ArgumentError if n < 0 + + return 1 if n <= 1 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(s.length) or O(n) +# Space complexity: O(s.length) or O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + + return s[s.length-1] << reverse(s[1..-2]) << s[0] end # Time complexity: ? -# Space complexity: ? +# Space complexity: O(1) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + # How would I do this?? end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return 0 if n == 0 + + return 2 + bunny(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(s.length) or O(n) +# Space complexity: O(1) def nested(s) - raise NotImplementedError, "Method not implemented" + return true if s.nil? + return false if s.length == 1 + + if s.length == 2 + if s == "()" + return true + else + return false + end + end + + nested(s[1..-2]) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(array.length) or O(n) +# Space complexity: O(1) def search(array, value) - raise NotImplementedError, "Method not implemented" + return false if array.length == 0 + + return true if array[0] == value + + search(array[1..-1], value) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(s.length) or O(n) +# Space complexity: O(1) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.length <= 1 + + return false if s[0] != s[-1] + + is_palindrome(s[1..-2]) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(log(n)) or O(log(m)) whichever is shorter +# Space complexity: O(log(n)) or O(log(m)) whichever is shorter def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + if (n % 10) == (m % 10) + if n < 10 || m < 10 + return 1 + else + return 1 + digit_match(n/10, m/10) + end + else + if n < 10 || m < 10 + return 0 + else + return 0 + digit_match(n/10, m/10) + end + end +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..4527714 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -7,77 +7,77 @@ it "will find the factorial of 0" do # Arrange num = 0 - + # Act answer = factorial(num) - + # Assert expect(answer).must_equal 1 end - + it "will find the factorial of 5" do # Arrange num = 5 - + # Act answer = factorial(num) - + # Assert expect(answer).must_equal 5*4*3*2*1 - + end - + it "will raise an ArgumentError if given a number not >= 0" do # Arrange num = -1 - + # Act-Assert expect { - answer = factorial(num) - }.must_raise ArgumentError - end + answer = factorial(num) + }.must_raise ArgumentError +end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "tac" end - + it "will reverse 'a'" do # Arrange string = "a" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "a" end - + it "will reverse empty string " do # Arrange string = "" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "" end it "will reverse 'apple'" do # Arrange string = "apple" - + # Act answer = reverse(string) - + # Assert expect(answer).must_equal "elppa" end @@ -88,208 +88,208 @@ it "will reverse 'cat'" do # Arrange string = "cat" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "tac" end - + it "will reverse 'a'" do # Arrange string = "a" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "a" end - + it "will reverse empty string " do # Arrange string = "" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "" end it "will reverse 'apple'" do # Arrange string = "apple" - + # Act answer = reverse_inplace(string) - + # Assert expect(answer).must_equal "elppa" end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 0 end - + it "returns 2 for 1 bunny" do # Arrange count = 1 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 2 end - + it "returns 100 for 50 bunnies" do # Arrange count = 50 - + # Act answer = bunny(count) - + # Assert expect(answer).must_equal 100 end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal true end - + it "will return true for a nested series of parens" do # Arrange string = "((()))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal true end - + it "will return false for a nested series of parens" do # Arrange string = "(()))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal false end - + it "will return false for an even length improperly nested series of parens" do # Arrange string = "(())))" - + # Act answer = nested(string) - + # Assert expect(answer).must_equal false end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" array = [] - + # Act answer = search(array, item) - + # 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"] - - # Act - answer = search(array, item) - - # Assert - expect(answer).must_equal true + # Arrange + item = "a" + array = ["b", "c", "a"] + + # Act + answer = search(array, item) + + # 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"] - + # 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"] + 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 + # Assert + expect(answer).must_equal true + end end -xdescribe "is_palindrome" do +describe "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 @@ -300,59 +300,59 @@ # Arrange num1 = 1072503891 num2 = 62530841 - + # Act answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 4 + + # 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 + + # 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 + + # Assert + expect(answer).must_equal 3 end it "returns 1 for (0, 0)" do # Arrange num1 = 0 num2 = 0 - + # Act answer = digit_match(num1, num2) - - # Assert - expect(answer).must_equal 1 + + # 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 + + # Assert + expect(answer).must_equal 1 end end