diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..7641b54 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,117 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def factorial(n) - raise NotImplementedError, "Method not implemented" + if n == 0 || n == 1 + return 1 + elsif n < 0 + raise ArgumentError + end + + until n == 1 do + return n * factorial(n-1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + end + + reversed_str = reverse(s[1..-1]) + reversed_str += s[0] + reversed_str end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + end + + return s[s.length-1] + reverse_inplace(s[0, s.length-1]) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return 0 + elsif n == 1 + return 2 + else + return 2 + bunny(n - 1) + end + + return end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def nested(s) - raise NotImplementedError, "Method not implemented" + if s.length.odd? + return false + elsif + s.length == 0 + return true + end + if s[0] != s[-1] + return nested(s[1..-2]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1) + +def search(array,value) + if array == [] + return false + elsif array[0] == value + return true + else + array.delete_at(0) + return search(array, value) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s.length == 0 || s.length == 1 + return true + elsif s[0] == s[-1] + return is_palindrome(s[1..-2]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + + +# Time complexity: O(n + m) +# Space complexity: O(1) + +def digit_match(n, m, c = 0) + if n.class != String || m.class != String + n = n.to_s + m = m.to_s + end + + checker = c + + until n.length == 0 || m.length == 0 + # binding.pry + if n[-1] == m[-1] + return digit_match(n.chop!, m.chop!, checker + 1) + else + return digit_match(n.chop!, m.chop!, checker) + end + end + return checker end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..1b2f5d4 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -2,357 +2,357 @@ require 'minitest/reporters' require "minitest/skip_dsl" require_relative '../lib/recursive-methods' +require "pry" describe "factorial" do 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 + 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 end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do 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 end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # 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