diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..dba5833 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,91 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: Linear where n is the count of the input number +# Space complexity: Linear because the stacktrace is taking up n amount of space, where n is the count of the input number def factorial(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError if n < 0 + if n == 1 || n == 0 + return 1 + else + return n * factorial(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: linear, O(n), where n is the length of the input string. The function is being called n times +# Space complexity: linear, O(n), where n is the length of the input string. + def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length == 1 || s.length == 0 + return s + else + return reverse_inplace(s[1..-1]) + s[0] + end end # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" end -# Time complexity: ? -# Space complexity: ? +# Time complexity: Linear, O(n), where n is the count of the input integer, n +# Space complexity: Same as time complexity def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return 0 + else + return 2 + bunny(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: worst case scenario, is O(n), where n is the length of the string +# Space complexity: same as time complexity def nested(s) - raise NotImplementedError, "Method not implemented" + if s.length == 0 + return true + elsif s[0] == "(" && s[-1] == ")" + return nested(s[1...-1]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: worst case scenario, is linear, O(n), where n is the length of the array +# Space complexity: same as time complexity def search(array, value) - raise NotImplementedError, "Method not implemented" + if array == [] + return false + end + if array[0] == value + return true + else + return search(array[1..-1], value) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: worst case scenario is linear, O(n), where n is the length of the string +# Space complexity: same as time complexity def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s == "" + return true + elsif s[0] == s[-1] + return is_palindrome(s[1...-1]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: logarithmic, or O(log10) since, worst case, the number is divided by 10 during each internal function call +# Space complexity: same as time complexity def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + temp1 = n % 10 + temp2 = m % 10 + count = 0 + if n < 1 || m < 1 + return count + elsif temp1 == temp2 + count += 1 + return count + digit_match(n / 10, m / 10) + else + return digit_match(n / 10, m / 10) + end +end diff --git a/specs/recursion_writing_spec.rb b/specs/recursion_writing_spec.rb index d8fb1ed..6361b6a 100644 --- a/specs/recursion_writing_spec.rb +++ b/specs/recursion_writing_spec.rb @@ -1,7 +1,7 @@ -require 'minitest/autorun' -require 'minitest/reporters' +require "minitest/autorun" +require "minitest/reporters" require "minitest/skip_dsl" -require_relative '../lib/recursive-methods' +require_relative "../lib/recursive-methods" describe "factorial" do it "will find the factorial of 0" do @@ -23,8 +23,7 @@ answer = factorial(num) # Assert - expect(answer).must_equal 5*4*3*2*1 - + expect(answer).must_equal 5 * 4 * 3 * 2 * 1 end it "will raise an ArgumentError if given a number not >= 0" do @@ -83,8 +82,7 @@ end end - -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +127,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -164,7 +162,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -210,7 +208,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -224,43 +222,43 @@ end it "will return true when looking for something in the array" do - # Arrange - item = "a" - array = ["b", "c", "a"] + # Arrange + item = "a" + array = ["b", "c", "a"] - # Act - answer = search(array, item) + # Act + answer = search(array, item) - # Assert - expect(answer).must_equal true + # 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"] - - # Act - answer = search(array, item) - - # Assert - expect(answer).must_equal true - end + 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) + + # 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 = "" @@ -304,8 +302,8 @@ # 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 @@ -316,8 +314,8 @@ # 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 @@ -328,7 +326,7 @@ # Act answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 3 + # Assert + expect(answer).must_equal 3 end -end \ No newline at end of file +end