From 46d948aa6a5ac0f8e1d0b5ef0b2ffa135d3a2131 Mon Sep 17 00:00:00 2001 From: "Noor B.A" Date: Tue, 3 Nov 2020 09:38:53 -0800 Subject: [PATCH] finalized methods --- Gemfile.lock | 36 +++++++++++++ lib/recursive-methods.rb | 93 ++++++++++++++++++++++++---------- test/recursion_writing_test.rb | 8 +-- 3 files changed, 106 insertions(+), 31 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 fbf6faa..dc799b3 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,88 @@ # 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,"unvailed number" if n < 0 + return 1 if n == 0 + 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 + + i = 1 + reversed_string = "" + while i <= s.length + reversed_string += s[-i] + i += 1 + end + return reversed_string end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + if s.length <= 1 + return s + end + + return s[-1] + reverse(s[1..-2]) + s[0] end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return 0 if n == 0 + return 2 if n == 1 + 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(n) +def nested(s, i = 1 ) + return true if s.empty? + if s[i+1] == "(" && s[-i] == ")" + return true + else + return false + end + + return nested(s, i - 1) 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: ? +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return s[0] == s[-1] && (s.length <= 2 || is_palindrome(s[1..-2])) + end -# Time complexity: ? -# Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(n) +def digit_match(n, m, i = 0, count = 0) + n.to_s + m.to_s + + if n[i] == m[i] + return count + 1 + else + return count + end + + return digit_match(n, m, i + 1, count) end \ No newline at end of file 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