From ea887051d30d24693ee0df7971f20a1c2ee4eb71 Mon Sep 17 00:00:00 2001 From: brilatimer Date: Sat, 16 Nov 2019 17:03:14 -0800 Subject: [PATCH 1/2] 3 problems complete, tests passing --- lib/recursive-methods.rb | 57 +++++++++++++++++++++++++++++----------- 1 file changed, 42 insertions(+), 15 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..1d7a722 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,76 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) because constant is being multiplied (no arrays) def factorial(n) - raise NotImplementedError, "Method not implemented" + # solve base case + if n < 0 + raise ArgumentError + end + if n == 0 + return 1 + end + if n <= 2 + return n + end + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + end + return [reverse(s[1, s.length - 1]), s[0]].join end +# substring on the left, first char on the right, put these into an arry and call .join +# https://apidock.com/ruby/Array/join -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1), no additional space used/no new arrays/strings created +def reverse_inplace(s, index = 0) + start_index = index + end_index = s.length - 1 - index + + # middle point has been found. this is basecase + if end_index <= start_index + return s + end + # swap start and end elements + temp = s[start_index] + s[start_index] = s[last_index] + s[last_index] = temp + + return reverse_inplace(s, index + 1) # this handles swapping middle end # Time complexity: ? # Space complexity: ? def bunny(n) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + raise NotImplementedError, "Method not implemented" end \ No newline at end of file From 359b9a4bdc56a0cca7f37ecf75aaea783f30b2a3 Mon Sep 17 00:00:00 2001 From: brilatimer Date: Sun, 17 Nov 2019 15:35:30 -0800 Subject: [PATCH 2/2] nested and bunny problems working and tested --- lib/recursive-methods.rb | 33 ++++++++++++++++++++++++--------- test/recursion_writing_test.rb | 8 ++++---- 2 files changed, 28 insertions(+), 13 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 1d7a722..78146d3 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -39,22 +39,37 @@ def reverse_inplace(s, index = 0) end # swap start and end elements temp = s[start_index] - s[start_index] = s[last_index] - s[last_index] = temp + s[start_index] = s[end_index] + s[end_index] = temp return reverse_inplace(s, index + 1) # this handles swapping middle end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(1) def bunny(n) - raise NotImplementedError, "Method not implemented" + if n < 0 + raise ArgumentError + end + if n == 0 # basecase + return n + end + return bunny(n - 1) + 2 end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1) +def nested(s, index = 0) # initalize index + start_index = index + end_index = s.length - 1 - index + if end_index < start_index + return true + end + if s[start_index] == '(' && s[end_index] == ')' + return nested(s, index + 1) + else + return false + end end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..8841792 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 @@ -164,7 +164,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = ""