From 528ff36df3c8983162a04509a509e17d36e797ee Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 16 Dec 2020 23:08:45 -0800 Subject: [PATCH 1/5] completed reverse in place, bunny, and nested methods --- lib/recursive-methods.rb | 67 ++++++++++++++++++++++++++++++---------- 1 file changed, 50 insertions(+), 17 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..28b2196 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,33 +1,66 @@ # 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 <= 1 + return 1 + else + return n * factorial(n-1) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# 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.chop) + end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + reverse_helper(0, s.length - 1, s) + return s end -# Time complexity: ? -# Space complexity: ? +def reverse_helper(i,j, string) + if i < j + temp = string[i] + string[i] = string[j] + string[j] = temp + reverse_helper(i + 1, j - 1, string) + end +end + +# 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: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def nested(s) - raise NotImplementedError, "Method not implemented" + nested_helper(0, s.length - 1, s) +end + +def nested_helper(i,j, string) + if i > j + return true + elsif string[i] == '(' && string[j] == ')' + return nested_helper(i + 1, j - 1, string) + else + return false + end end # Time complexity: ? @@ -42,8 +75,8 @@ def is_palindrome(s) raise NotImplementedError, "Method not implemented" end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def digit_match(n, m) raise NotImplementedError, "Method not implemented" end \ No newline at end of file From e9050d89906ca553b8bcce17ef99bf60d9a8d193 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 16 Dec 2020 23:09:02 -0800 Subject: [PATCH 2/5] unskipped nested test --- test/recursion_writing_test.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 3b30725..ba7bc41 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 = "" From bcc88d43adaa0a8a35c5980044c1e35e6d47e0d6 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 16 Dec 2020 23:19:40 -0800 Subject: [PATCH 3/5] completed palindrome method and unskipped test --- Gemfile.lock | 36 ++++++++++++++++++++++++++++++++++ lib/recursive-methods.rb | 20 ++++++++++++++++--- test/recursion_writing_test.rb | 2 +- 3 files changed, 54 insertions(+), 4 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 28b2196..a204411 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -69,10 +69,24 @@ def search(array, value) raise NotImplementedError, "Method not implemented" end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + if s.length < 2 + return true + else + palindrome_helper(0,s.length - 1, s) + end +end + +def palindrome_helper(i,j,string) + if i > j + return true + elsif string[i] == string[j] + palindrome_helper(i+1, j-1, string) + else + return false + end end # Time complexity: O(n^2) diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index ba7bc41..0a4de73 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -263,7 +263,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" From be3a7a18f1c8ca633d7e2f8936ae13e124f0d72a Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Wed, 16 Dec 2020 23:34:11 -0800 Subject: [PATCH 4/5] completed and unskipped test for search --- lib/recursive-methods.rb | 22 ++++++++++++++++++---- test/recursion_writing_test.rb | 4 ++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index a204411..e339d98 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -66,7 +66,13 @@ def nested_helper(i,j, string) # Time complexity: ? # Space complexity: ? def search(array, value) - raise NotImplementedError, "Method not implemented" + return false if array.length == 0 + if array[0] == value + return true + else + search(array[1..-1], value) + end + return false end # Time complexity: O(n) @@ -89,8 +95,16 @@ def palindrome_helper(i,j,string) end end -# Time complexity: O(n^2) -# Space complexity: O(n^2) +# Time complexity: O(nm) +# Space complexity: O(nm) def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + matches = 0 + if n%10 == m%10 + matches += 1 + end + if n < 10 || m < 10 + return matches + else + digit_match(n/10,m/10) + end end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 0a4de73..4984dad 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -213,7 +213,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -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 1237a32b91dfd3fa502093f54ace51d330c218d9 Mon Sep 17 00:00:00 2001 From: Jessica Chan Date: Thu, 17 Dec 2020 03:00:40 -0800 Subject: [PATCH 5/5] completed digit match --- lib/recursive-methods.rb | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index e339d98..03367a8 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -11,7 +11,7 @@ def factorial(n) end end -# Time complexity: O(n) +# Time complexity: O(n^2) # Space complexity: O(n^2) def reverse(s) if s.length <= 1 @@ -63,14 +63,14 @@ def nested_helper(i,j, string) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) +# Space complexity: O(n^2) def search(array, value) return false if array.length == 0 if array[0] == value return true else - search(array[1..-1], value) + return search(array[1..-1], value) end return false end @@ -95,16 +95,17 @@ def palindrome_helper(i,j,string) end end -# Time complexity: O(nm) -# Space complexity: O(nm) +# Time complexity: O(logn) +# Space complexity: O(logn) def digit_match(n, m) - matches = 0 - if n%10 == m%10 - matches += 1 + if n == 0 && m == 0 + return 1 + elsif n <= 1 || m <= 1 + return 0 end - if n < 10 || m < 10 - return matches + if n%10 == m%10 + return 1 + digit_match(n/10, m/10) else - digit_match(n/10,m/10) + return digit_match(n/10, m/10) end end \ No newline at end of file