From e3ae03a0fed089a1adf93eeec5ac0edf5d5cfc22 Mon Sep 17 00:00:00 2001 From: Vi Date: Mon, 11 Nov 2019 19:55:30 -0800 Subject: [PATCH 1/2] 6/8 complete, two left --- lib/recursive-methods.rb | 86 +++++++++++++++++++++++++--------- test/recursion_writing_test.rb | 10 ++-- 2 files changed, 69 insertions(+), 27 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..091f9b4 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,27 +1,52 @@ # 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" + if n < 0 + raise ArgumentError + elsif n <= 1 + return 1 + else + return n * factorial(n - 1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" -end + reversed_string = "" + if s.length >= 1 + reversed_string = reverse(s[1..-1]) + reversed_string += s[0] + else + return reversed_string + end +end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1) +def reverse_inplace(s, i = 0, j = -1) + n = s.length + if i == n / 2 + return s + else + letter = s[i] + s[i] = s[j] + s[j] = letter + reverse_inplace(s, i + 1, j - 1) + end end -# Time complexity: ? -# Space complexity: ? -def bunny(n) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def bunny(n, ears = 0) + if ears == n + n + return ears + else + ears += n + bunny(n, ears) + end end # Time complexity: ? @@ -30,16 +55,33 @@ def nested(s) raise NotImplementedError, "Method not implemented" end -# Time complexity: ? +# Time complexity: O(n) # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" -end + new_array_minus_one = 0..-2 -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if array.empty? + return false + elsif array[-1] == value + return true + else + search(array[new_array_minus_one], value) + end +end + +# Time complexity: O(n) +# Space complexity: O(1) +def is_palindrome(s, i = 0, j = -1) + n = s.length + + if i >= s.length + j + return true + elsif + s[i] != s[j] + return false + elsif s[i] == s[j] + is_palindrome(s, i + 1, j - 1) + end end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..974ddde 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -38,7 +38,7 @@ end end -xdescribe "reverse" do +describe "reverse" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -84,7 +84,7 @@ end -xdescribe "reverse_in_place" do +describe "reverse_in_place" do it "will reverse 'cat'" do # Arrange string = "cat" @@ -129,7 +129,7 @@ end end -xdescribe "bunny" do +describe "bunny" do it "returns 0 for 0 bunnies" do # Arrange count = 0 @@ -210,7 +210,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -260,7 +260,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" From b8c6b7dc2c9033ff447bc41e160f0d58064f1c59 Mon Sep 17 00:00:00 2001 From: vi reigosa Date: Thu, 14 Nov 2019 08:53:34 -0800 Subject: [PATCH 2/2] nested method written --- lib/recursive-methods.rb | 23 ++++++++++++++++------- test/recursion_writing_test.rb | 2 +- 2 files changed, 17 insertions(+), 8 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 091f9b4..6ba01a5 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,7 +1,7 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(n^2) +# Space complexity: O(n^2) def factorial(n) if n < 0 raise ArgumentError @@ -49,10 +49,19 @@ def bunny(n, ears = 0) end end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def nested(s, low = 0, high = (s.length - 1)) + if s.length.odd? + return false + elsif low < high + if s[low] != "(" && s[high] != ")" + return false + end + return nested(s, low + 1, high - 1) + else + return true + end end # Time complexity: O(n) @@ -87,5 +96,5 @@ def is_palindrome(s, i = 0, j = -1) # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 974ddde..416f05a 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -164,7 +164,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = ""