diff --git a/.DS_Store b/.DS_Store new file mode 100644 index 0000000..5008ddf Binary files /dev/null and b/.DS_Store differ diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..03505f1 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,103 @@ # 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 || n == 0 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n/2) >> O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length >= 2 + temp_char = s[0] + s[0] = s[-1] + s[-1] = temp_char + return s[0] + reverse(s[1...-1]) + s[-1] + else + return s + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n/2) >> O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length >= 2 + temp_char = s[0] + s[0] = s[-1] + s[-1] = temp_char + return s[0] + reverse(s[1...-1]) + s[-1] + else + return s + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError if n < 0 + return 0 if n == 0 + return 2 + bunny(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) 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] == ')' + nested(s[1..-2]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) - worst case +# Space complexity: O(n) - worst case def search(array, value) - raise NotImplementedError, "Method not implemented" + return false if array[0] == nil + + return true if array[0] == value + + search(array[1..-1], value) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n/2) => O(n) +# Space complexity: O(n/2) => O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.length <=1 + + if s[0] == s[-1] + is_palindrome(s[1..-2]) + else + return false + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) n is the length of the shortest array +# Space complexity: O(n) def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + if n.class == Integer + n = n.to_s.split("") # [1, 0, 7, 2, 5, 0, 3, 8, 9, 1] + end + + if m.class == Integer + m = m.to_s.split("") # [6, 2, 5, 3 ,0, 8, 4, 1] + end + + return n.length if n[-1] == nil || n[-1] == "match" + + return m.length if m[-1] == nil || m[-1] == "match" + + if n[-1] == m[-1] + n.insert(0, "match") + m.insert(0, "match") + end + + digit_match(n[0..-2], m[0..-2]) + +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..2561c59 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -3,34 +3,36 @@ require "minitest/skip_dsl" require_relative '../lib/recursive-methods' +Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new + 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) @@ -38,321 +40,321 @@ 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