diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..4350279 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,120 @@ # 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) + +# factorial(n) Write a method factorial that accepts an integer +# parameter n and that uses recursion to compute and return the +# value of n factorial (also known as n!). + +# e.g. fact(4) = 4 * 3 * 2 * 1 = 24 + def factorial(n) - raise NotImplementedError, "Method not implemented" + if (n < 0) + raise ArgumentError + elsif n == 1 || n == 0 + return 1 + else + return n * factorial(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) + +# reverse(s) Write a method reverse that accepts a string as a +# parameter and then returns the reverse of the string by +# reversing all letters and all words in the string. + +# e.g. reverse("hello world") will return "dlrow olleh" def reverse(s) - raise NotImplementedError, "Method not implemented" + if (s != "") + return s[-1] + reverse(s[0...-1]) + else + return "" + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + return reverse_inplace_internal(s, 0, s.length - 1) end -# Time complexity: ? -# Space complexity: ? +def reverse_inplace_internal(s, start_i, end_i) + if (start_i >= end_i) + return s + else + temp = s[start_i] + s[start_i] = s[end_i] + s[end_i] = temp + + start_i += 1 + end_i -= 1 + + return reverse_inplace_internal(s, start_i, end_i) + end +end + +# Time complexity: O(n) +# Space complexity: O(n) 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 end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def nested(s) - raise NotImplementedError, "Method not implemented" + if s == "" + return true + elsif s.length == 1 + return false + else + outside_nested = s[0] == "(" && s[-1] == ")" + return outside_nested && nested(s[1...-1]) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def search(array, value) - raise NotImplementedError, "Method not implemented" + if array.count() == 0 + return false + else + return array.pop() == value || search(array, value) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s == "" + return true + elsif s.length == 1 + return true + else + return s[0] == s[-1] && is_palindrome(s[1...-1]) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file + if n == 0 && m == 0 + return 1 + elsif n == 0 && m > 0 || m == 0 && n > 0 + return 0 + elsif n % 10 == m % 10 + return 1 + digit_match(n / 10, m / 10) + elsif n / 10 > 0 || m / 10 > 0 + return digit_match(n / 10, m / 10) + else + return 0 + end +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..8fc5ba1 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.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 @@ -38,7 +37,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -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 = "" @@ -295,7 +293,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 @@ -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,10 +326,10 @@ # 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 @@ -340,10 +338,10 @@ # 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 @@ -352,7 +350,7 @@ # Act answer = digit_match(num1, num2) - # Assert - expect(answer).must_equal 1 + # Assert + expect(answer).must_equal 1 end end