From 48f1be84d1c79d10808bbe7fc760564768664c65 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Mon, 2 Nov 2020 18:57:31 -0800 Subject: [PATCH 1/5] created factorial method --- lib/recursive-methods.rb | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..548ed57 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,9 +1,16 @@ # 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 + + if n == 0 #base case + return 1 + else + return n * factorial(n-1) + end end # Time complexity: ? From 357ab894f465f8e71fd1544f4a8a48aa5e5d6316 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Mon, 2 Nov 2020 19:04:54 -0800 Subject: [PATCH 2/5] created reverse method and added time and space complexity --- lib/recursive-methods.rb | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 548ed57..a3b5849 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -13,10 +13,14 @@ def factorial(n) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + else + return s[-1] + reverse(s[1..-2]) + s[0] + end end # Time complexity: ? From 28b9c24cd253b56ff2472b3d72adb91c63bc49b8 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Mon, 2 Nov 2020 20:01:17 -0800 Subject: [PATCH 3/5] created bunny method --- lib/recursive-methods.rb | 18 ++++++++++++++---- 1 file changed, 14 insertions(+), 4 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index a3b5849..36bb584 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -23,16 +23,26 @@ def reverse(s) end end + + # Time complexity: ? # Space complexity: ? def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + else + return reverse_inplace(s[1..-1]) + s[0] + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + if n == 0 + return 0 + else + return 2 + bunny(n-1) + end end # Time complexity: ? From c82dfd8f897304eeb21669fc821ab08e91d27588 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Mon, 2 Nov 2020 20:28:13 -0800 Subject: [PATCH 4/5] created is_palindrom method --- lib/recursive-methods.rb | 38 ++++++++++++++++++++++++++-------- test/recursion_writing_test.rb | 8 +++---- 2 files changed, 33 insertions(+), 13 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 36bb584..f76c07b 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -45,22 +45,42 @@ def bunny(n) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def nested(s) - raise NotImplementedError, "Method not implemented" + if s.empty? + return true + elsif s[0] == s[-1] + return false + else + return nested(s[1..-2]) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def search(array, value) - raise NotImplementedError, "Method not implemented" + if array.empty? + return false + elsif array[0] == value + return true + else + return search(array[1..-1], value) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s.length == 1 || s.empty? + return true + end + + if s[0] == s[-1] + is_palindrome(s[1..-2]) + else + return false + end end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 3b30725..4984dad 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -167,7 +167,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" @@ -213,7 +213,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -263,7 +263,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" @@ -298,7 +298,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 From db0a104e40f6d62f11d846bf2b0c1a03ff0f35e3 Mon Sep 17 00:00:00 2001 From: Lina Do Date: Tue, 8 Dec 2020 15:09:17 -0800 Subject: [PATCH 5/5] went over each recursion problem --- Gemfile.lock | 36 +++++++++++++++++ lib/recursive-methods.rb | 86 ++++++++++++++++++++++++++++++---------- 2 files changed, 102 insertions(+), 20 deletions(-) create mode 100644 Gemfile.lock diff --git a/Gemfile.lock b/Gemfile.lock new file mode 100644 index 0000000..630ea95 --- /dev/null +++ b/Gemfile.lock @@ -0,0 +1,36 @@ +GEM + remote: https://rubygems.org/ + specs: + ansi (1.5.0) + builder (3.2.4) + coderay (1.1.3) + method_source (1.0.0) + minitest (5.14.2) + minitest-reporters (1.4.2) + ansi + builder + minitest (>= 5.0) + ruby-progressbar + minitest-skip (0.0.3) + minitest (~> 5.0) + minitest-spec (0.0.2.1) + minitest (>= 3.0) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) + rake (13.0.1) + ruby-progressbar (1.10.1) + +PLATFORMS + ruby + +DEPENDENCIES + minitest + minitest-reporters + minitest-skip + minitest-spec + pry + rake + +BUNDLED WITH + 2.1.4 diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index f76c07b..2ed4c7d 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -23,16 +23,27 @@ def reverse(s) end end +# another way to do reverse +# if s.length <= 1 +# return s +# else +# return reverse_inplace(s[1..-1]) + s[0] +# end + # Time complexity: ? # Space complexity: ? -def reverse_inplace(s) - if s.length <= 1 +def reverse_inplace(s, low = 0, high = s.length - 1) + if low >= high return s else - return reverse_inplace(s[1..-1]) + s[0] + temp = s[low] + s[low] = s[high] + s[high] = temp + return reverse_inplace(s, low + 1, high - 1) end + end # Time complexity: O(n) @@ -47,44 +58,79 @@ def bunny(n) # Time complexity: O(n^2) # Space complexity: O(n^2) -def nested(s) - if s.empty? +def nested(s, low = 0, high = s.length - 1) + # if s.empty? + # return true + # elsif s[0] == s[-1] + # return false + # else + # return nested(s[1..-2]) + # end + # + if low > high return true - elsif s[0] == s[-1] + elsif s[low] == s[high] return false else - return nested(s[1..-2]) + nested(s, low + 1, high - 1) end + end # Time complexity: O(n^2) # Space complexity: O(n^2) -def search(array, value) - if array.empty? +def search(array, value, index = 0) + # if array.empty? + # return false + # elsif array[0] == value + # return true + # else + # return search(array[1..-1], value) + # end + + if array.empty? || index > array.length - 1 return false - elsif array[0] == value + elsif array[index] == value return true else - return search(array[1..-1], value) + search(array, value, index + 1) end end # Time complexity: O(n^2) # Space complexity: O(n^2) -def is_palindrome(s) - if s.length == 1 || s.empty? +def is_palindrome(s, left = 0, right = s.length - 1) + # if s.length == 1 || s.empty? + # return true + # end + # + # if s[0] == s[-1] + # is_palindrome(s[1..-2]) + # else + # return false + # end + if left > right return true - end - - if s[0] == s[-1] - is_palindrome(s[1..-2]) - else + elsif s[left] != s[right] return false + else + is_palindrome(s, left + 1, right - 1 ) end + end # Time complexity: ? # Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" +def digit_match(n, m, match_count = 0) + + raise ArgumentError if n < 0 || n < 0 + + match_count += 1 if n % 10 == m % 10 + + if n < 10 || m < 10 + return match_count + else + return digit_match(n/10, m/10, match_count) + end + end \ No newline at end of file