From 2e86f80c31d5c01091cdbc13a80c0b8c37690a70 Mon Sep 17 00:00:00 2001 From: Iris Lux Date: Mon, 2 Nov 2020 23:06:53 -0800 Subject: [PATCH 1/3] completed assignment --- Gemfile.lock | 36 ++++++++++ lib/recursive-methods.rb | 117 ++++++++++++++++++++++++--------- test/recursion_writing_test.rb | 33 ++++++++-- 3 files changed, 152 insertions(+), 34 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..81086c7 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,49 +1,106 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), where n is size of number +# Space complexity: O(n) def factorial(n) - raise NotImplementedError, "Method not implemented" + raise ArgumentError unless n >= 0 + + return 1 if n == 0 + + return n * factorial(n-1) + end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: 0(n^2) where n is length od string +# Space complexity: 0(n^2) where n is length of string + def reverse(s) - raise NotImplementedError, "Method not implemented" + return s if s.length < 2 + return s[-1] + reverse(s[1..-2]) + s[0] end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" + +# Time complexity: o(n) where n is length of string +# Space complexity: o(n) +def reverse_inplace(s, first = 0, last = s.length - 1) + return s if first == last || s.empty? + s[first], s[last] = s[last], s[first] + return reverse_inplace(s, first + 1, last - 1) end -# Time complexity: ? -# Space complexity: ? + +# Time complexity: O(n) where n is size of number +# Space complexity: O(n) def bunny(n) - raise NotImplementedError, "Method not implemented" + return n if n <= 0 + + return bunny(n-1) + 2 +end + +# Time complexity: o(n) where n is length of string +# Space complexity: o(n) + +def nested(s, first = 0, sub_length = s.length) + + return false if sub_length == 1 + + return true if first == sub_length + + return (s[first] == '(' && s[sub_length-1] == ')') ? nested(s, first + 1, sub_length - 1) : false + +end + +# Time complexity: O(n) where n is length of array +# Space complexity: O(n) + +def search(array, value, i = 0) + return true if array[i] == value + + return false if i == array.length + + return search(array, value, i + 1) end -# Time complexity: ? -# Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) where n is the length of string +# Space complexity: O(n) where n is the length of string +# +def is_palindrome(s, first = 0, sub_length = s.length) + + return true if first == sub_length || first == sub_length - 1 + + return (s[first] == s[sub_length-1]) ? is_palindrome(s, first + 1, sub_length - 1) : false + end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" + +# Time complexity: O(n) where n is the largest number +# Space complexity: O(n) +def digit_match(n, m, count = 0) + if n/10 == 0 && m/10 == 0 + return n == m ? count + 1 : count + end + if n/10 == 0 + return m % 10 == n ? count + 1 : count + elsif m/10 == 0 + return n % 10 == m ? count + 1 : count + else + return n%10 == m%10 ? digit_match(n/10, m/10, count + 1) : digit_match(n/10, m/10, count) + end end -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" +def fib(n) + return n if n <= 1 + return fib(n - 1) + fib(n - 2) end -# Time complexity: ? -# Space complexity: ? -def digit_match(n, m) - raise NotImplementedError, "Method not implemented" -end \ No newline at end of file +# def fib_non_rec(n) +# seq = Array.new(n+2) +# +# seq[0] = 0 +# seq[1] = 1 +# +# (2..n).each { |i| seq[i] = seq[i - 1] + seq[i - 2]} +# +# return seq[n] +# end \ No newline at end of file diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 3b30725..cd7b746 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 = "" @@ -296,9 +296,20 @@ # Assert expect(answer).must_equal false end + + it "will return true for a even palindrome" do + # Arrange + string = "baiiab" + + # Act + answer = is_palindrome(string) + + # Assert + expect(answer).must_equal true + end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 @@ -359,3 +370,17 @@ expect(answer).must_equal 1 end end + +describe "fib" do + it "returns 3 when passed 4" do + expect(fib(4)).must_equal 3 + end + + it "returns 13 when passed 7" do + expect(fib(7)).must_equal 13 + end + + it "returns 0 when passed 0" do + expect(fib(0)).must_equal 0 + end +end \ No newline at end of file From a5da696a5345a7767118f3979da3694d2203e74b Mon Sep 17 00:00:00 2001 From: Iris Lux Date: Tue, 3 Nov 2020 01:00:28 -0800 Subject: [PATCH 2/3] corrected o(n) for digit match --- lib/recursive-methods.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 81086c7..874b63b 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -74,7 +74,7 @@ def is_palindrome(s, first = 0, sub_length = s.length) end -# Time complexity: O(n) where n is the largest number +# Time complexity: O(logn) where n is the largest number # Space complexity: O(n) def digit_match(n, m, count = 0) if n/10 == 0 && m/10 == 0 @@ -87,6 +87,7 @@ def digit_match(n, m, count = 0) else return n%10 == m%10 ? digit_match(n/10, m/10, count + 1) : digit_match(n/10, m/10, count) end + end def fib(n) From f6ae81f1a85b31fabac80e24c18edd2a47b063fb Mon Sep 17 00:00:00 2001 From: Iris Lux Date: Tue, 3 Nov 2020 01:06:24 -0800 Subject: [PATCH 3/3] fixed o(n) for space --- lib/recursive-methods.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 874b63b..b59b417 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -75,11 +75,12 @@ def is_palindrome(s, first = 0, sub_length = s.length) # Time complexity: O(logn) where n is the largest number -# Space complexity: O(n) +# Space complexity: O(logn) def digit_match(n, m, count = 0) if n/10 == 0 && m/10 == 0 return n == m ? count + 1 : count end + if n/10 == 0 return m % 10 == n ? count + 1 : count elsif m/10 == 0 @@ -87,7 +88,6 @@ def digit_match(n, m, count = 0) else return n%10 == m%10 ? digit_match(n/10, m/10, count + 1) : digit_match(n/10, m/10, count) end - end def fib(n)