From 4e003730ee9aea85bd409bc93e1611898f71a625 Mon Sep 17 00:00:00 2001 From: Diana Date: Fri, 15 Nov 2019 17:23:03 -0800 Subject: [PATCH 1/2] will skip nested(s) function for now --- lib/recursive-methods.rb | 34 +++++++++++++++++++++------------- test/recursion_writing_test.rb | 6 +++--- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..3a487dd 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,33 +1,41 @@ # 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 + end + return 1 if n == 0 + return n * factorial(n-1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) +# I think this is reversing the string in place? def reverse(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + return reverse(s[1..-1]) + s[0] end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + return s if s.length <= 1 + return reverse_inplace(s[1..-1]) + s[0] end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return n if n == 0 + return 2 + bunny(n-1) end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + return true if s.length == 0 end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..36e73c4 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 From 6d404547a295068e0fe1a73e1f6b6a60a65c432e Mon Sep 17 00:00:00 2001 From: Diana Date: Fri, 15 Nov 2019 18:52:01 -0800 Subject: [PATCH 2/2] need some help with nested and digit_match --- lib/recursive-methods.rb | 50 +++++++++++++++++++++++++++++++--------- 1 file changed, 39 insertions(+), 11 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 3a487dd..9071afc 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,5 +1,5 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. - +require 'pry' # Time complexity: O(n) # Space complexity: O(n) def factorial(n) @@ -34,24 +34,52 @@ def bunny(n) # Time complexity: ? # Space complexity: ? +# NOT PASSING TESTS def nested(s) return true if s.length == 0 + # need to check if s = even number of paren-pairs() + # return true if s.length % 2 == 0 end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def search(array, value, i = 0) + return false if i >= array.length + return true if array[i] == value + return search(array, value, i + 1) end -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +# ONLY PASSING 2 TESTS +def is_palindrome(s, l = 0, r = s.length-1 ) + if s[l] == s[r] + return true + else + return false + end + + while l < s.length + return is_palindrome(s, l + 1, r - 1) + end end # Time complexity: ? # Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" +# NOT PASSING TESTS +def digit_match(n, m, i = -1) +# i want to check each index from the end and keep track of matching values + n = n.to_s + m = m.to_s + tracker = 0 + + if n[i] == m[i] + tracker += 1 + else + tracker += 0 + end + + return tracker if i < -(n.length) + + return digit_match(n, m, i - 1) end \ No newline at end of file