diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b59a4dc --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.idea/ +.rakeTasks \ No newline at end of file diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..8f50b97 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,115 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: n +# Space complexity: n def factorial(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError if n < 0 + return 1 if n == 0 + return 1 if n == 1 + return n * factorial(n - 1) end -# Time complexity: ? -# Space complexity: ? -def reverse(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: n^2 +# Space complexity: n^2 +def reverse(s) #was the answer to this supposed to be different from the one below?? + if s.length == 0 + return s + else + return reverse(s[1..-1]) +s[0] + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: n +# Space complexity: n def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + #fix - add a few default arguments + #add optional arguments + if s.length == 0 + return s + else + return reverse(s[1..-1]) + s[0] + end + + #Try Iterator to Recusion... + # reverse_index = -1 + # count = 0 + # length = s.length + # + # while count < length / 2 + # temp = s[count] + # s[count] = s[reverse_index] + # s[reverse_index] = temp + # + # count += 1 + # reverse_index -= 1 + # end + # return s end -# Time complexity: ? -# Space complexity: ? +#*** +# Could not for the life of me +# figure out how to do this recursively based +# on the tests provided. +# Didn't even use a loop to convert...>_< +#*** +# Time complexity: n +# Space complexity: n def bunny(n) - raise NotImplementedError, "Method not implemented" + return 0 if n == 0 + if n > 0 + return bunny(n - 1) + 2 + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: n^2 +# Space complexity: n^2 def nested(s) - raise NotImplementedError, "Method not implemented" + return true if s.length == 0 + return false if s.length % 2 != 0 + 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" + return false if array.length == 0 + if array[0] == value + return true + else + return search(array[1..-1], value) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: n +# Space complexity: n def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.length == 0 + if s[0] == s[-1] + return is_palindrome(s[1..-2]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: n +# Space complexity: n +# +# how many calls +# how expensive is each call def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + if n < 10 || m < 10 + last_digit_m = m % 10 + last_digit_n = n % 10 + return last_digit_m == last_digit_n ? 1 : 0 + else + last_digit_m = m % 10 + last_digit_n = n % 10 + match = last_digit_m == last_digit_n ? 1 : 0 + return digit_match(n / 10, m / 10) + match + end end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 3b30725..088e8d6 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -167,8 +167,8 @@ end end -xdescribe "nested" do - it "will return true for empystring" do +describe "nested" do + it "will return true for empty string" do # Arrange string = "" @@ -213,7 +213,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -263,7 +263,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" @@ -298,7 +298,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 @@ -311,7 +311,7 @@ expect(answer).must_equal 4 end - it "returns 0 for nonmatching numbers" do + it "returns 0 for non matching numbers" do # Arrange num1 = 0 num2 = 62530841 @@ -358,4 +358,24 @@ # Assert expect(answer).must_equal 1 end + + it "returns 0 for (107, 6)" do + # Arrange + + # Act + expect(digit_match(107, 6)).must_equal 0 + expect(digit_match(6, 107)).must_equal 0 + expect(digit_match(0, 0)).must_equal 1 + expect(digit_match(2, 2)).must_equal 1 + expect(digit_match(7, 6)).must_equal 0 + expect(digit_match(10, 0)).must_equal 1 + expect(digit_match(10, 1)).must_equal 0 + + expect(digit_match(1007, 16)).must_equal 0 + expect(digit_match(1007, 27)).must_equal 1 + + + # Assert + # expect(answer).must_equal 0 + end end