From 55b22d6c31e9840d1e2b4e6a926d1ab361bca2a8 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 11 Nov 2019 16:37:48 -0800 Subject: [PATCH 1/5] half way done --- lib/recursive-methods.rb | 44 +++++++++++++++++++++++----------- test/recursion_writing_test.rb | 8 +++---- 2 files changed, 34 insertions(+), 18 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..74be721 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,33 +1,49 @@ # 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.new("BAD") if n < 0 + return 1 if n == 0 + return n if n == 1 + 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" + return s if s.length <= 1 + return reverse(s[1..-1]) + s[0] end -# Time complexity: ? -# Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n) +# Space complexity: O(1) +def reverse_inplace(s, i = 0) + return s if i == s.length / 2 + s[i], s[s.length - i - 1] = s[s.length - i - 1], s[i] + s = reverse_inplace(s, i += 1) end # Time complexity: ? # Space complexity: ? def bunny(n) - raise NotImplementedError, "Method not implemented" + return 0 if n <= 0 + return 2 + bunny(n - 1) end # Time complexity: ? # Space complexity: ? def nested(s) - raise NotImplementedError, "Method not implemented" + return false if s.length % 2 != 0 + return true if s == "" + return true if s == "()" + center_l = s.length / 2 - 1 + center_r = center_l + 1 + if s[center_l] + s[center_r] != "()" + return false + else + next_string = s[center_l - 1] + s[center_r + 1] + end + return nested(next_string) end # Time complexity: ? @@ -46,4 +62,4 @@ def is_palindrome(s) # Space complexity: ? def digit_match(n, m) raise NotImplementedError, "Method not implemented" -end \ No newline at end of file +end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 820810e..8841792 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 @@ -164,7 +164,7 @@ end end -xdescribe "nested" do +describe "nested" do it "will return true for empystring" do # Arrange string = "" From 2d5d20d659e6a517f7d05a04af3b13e1939c34fb Mon Sep 17 00:00:00 2001 From: geli-gel Date: Mon, 11 Nov 2019 17:37:40 -0800 Subject: [PATCH 2/5] i'll do the last one later --- lib/recursive-methods.rb | 32 +++++++++++++++++++++----------- test/recursion_writing_test.rb | 18 +++++++++--------- 2 files changed, 30 insertions(+), 20 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 74be721..d504758 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,3 +1,4 @@ +require 'pry' # Authoring recursive algorithms. Add comments including time and space complexity for each method. # Time complexity: O(n) # Space complexity: O(n) @@ -16,22 +17,22 @@ def reverse(s) end # Time complexity: O(n) -# Space complexity: O(1) +# Space complexity: O(n) def reverse_inplace(s, i = 0) return s if i == s.length / 2 s[i], s[s.length - i - 1] = s[s.length - i - 1], s[i] s = reverse_inplace(s, i += 1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def bunny(n) return 0 if n <= 0 return 2 + bunny(n - 1) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n^2) since using .length? +# Space complexity: O(n) def nested(s) return false if s.length % 2 != 0 return true if s == "" @@ -46,16 +47,25 @@ def nested(s) return nested(next_string) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def search(array, value) - raise NotImplementedError, "Method not implemented" + return false if array.empty? + return true if array[0] == value + new_array = array[1..-1] + return search(array[1..-1], value) end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n) def is_palindrome(s) - raise NotImplementedError, "Method not implemented" + return true if s.length <= 1 + if s[0] != s[-1] + return false + else + next_string = s[1..-2] + end + return is_palindrome(next_string) end # Time complexity: ? diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 8841792..157a9ab 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -210,7 +210,7 @@ end end -xdescribe "search" do +describe "search" do it "will return false for empty array" do # Arrange item = "a" @@ -224,15 +224,15 @@ end it "will return true when looking for something in the array" do - # Arrange - item = "a" - array = ["b", "c", "a"] + # Arrange + item = "a" + array = ["b", "c", "a"] - # Act - answer = search(array, item) + # Act + answer = search(array, item) - # Assert - expect(answer).must_equal true + # Assert + expect(answer).must_equal true end it "will return false when looking for something not in the array" do @@ -260,7 +260,7 @@ end end -xdescribe "is_palindrome" do +describe "is_palindrome" do it "will return true for emptystring" do # Arrange string = "" From a8984e4d43b5bb2ea59dba7db01004302307c7b1 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Tue, 12 Nov 2019 10:41:08 -0800 Subject: [PATCH 3/5] it's doing SOMETHING --- lib/recursive-methods.rb | 12 +++++++++++- test/recursion_writing_test.rb | 2 +- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index d504758..75ba972 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -71,5 +71,15 @@ def is_palindrome(s) # Time complexity: ? # Space complexity: ? def digit_match(n, m) - raise NotImplementedError, "Method not implemented" + n = n.to_s + m = m.to_s + return 0 if n == nil || m == nil + match = n[-1] == m[-1] + if match && n.length == 1 + return 1 + elsif n.length <= 1 || m.length <= 1 # || n == nil || m == nil + return 0 + digit_match(n[0..-2].to_i, m[0..-2].to_i) + else + return 1 + digit_match(n[0..-2].to_i, m[0..-2].to_i) + end end diff --git a/test/recursion_writing_test.rb b/test/recursion_writing_test.rb index 157a9ab..b19c650 100644 --- a/test/recursion_writing_test.rb +++ b/test/recursion_writing_test.rb @@ -295,7 +295,7 @@ end end -xdescribe "digit_match" do +describe "digit_match" do it "returns 4 for 1072503891 and 62530841" do # Arrange num1 = 1072503891 From 275a391f378b3f22ceeed731f89f63a05d4940e0 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Tue, 12 Nov 2019 18:57:58 -0800 Subject: [PATCH 4/5] i did something and now it passes the tests! --- lib/recursive-methods.rb | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 75ba972..b33bd1e 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -70,14 +70,22 @@ def is_palindrome(s) # Time complexity: ? # Space complexity: ? +# 1072503891 and +# 62530841 +# ..x!!xx!x! + +# 10725 +# 625 def digit_match(n, m) n = n.to_s m = m.to_s - return 0 if n == nil || m == nil + return 0 if n == nil || m == nil || n.length == 0 || m.length == 0 match = n[-1] == m[-1] - if match && n.length == 1 - return 1 - elsif n.length <= 1 || m.length <= 1 # || n == nil || m == nil + if match && (n.length == 1 || m.length == 1) + return 1 + elsif !match && (n.length == 1 || m.length == 1) + return 0 + elsif !match return 0 + digit_match(n[0..-2].to_i, m[0..-2].to_i) else return 1 + digit_match(n[0..-2].to_i, m[0..-2].to_i) From 19abb7ac8387232e3777109561db0ce821f86790 Mon Sep 17 00:00:00 2001 From: geli-gel Date: Tue, 12 Nov 2019 19:03:01 -0800 Subject: [PATCH 5/5] added big O to last method --- lib/recursive-methods.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index b33bd1e..2d59b3d 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -68,14 +68,8 @@ def is_palindrome(s) return is_palindrome(next_string) end -# Time complexity: ? -# Space complexity: ? -# 1072503891 and -# 62530841 -# ..x!!xx!x! - -# 10725 -# 625 +# Time complexity: O(n^2) since it uses .length in each loop? +# Space complexity: O(n) def digit_match(n, m) n = n.to_s m = m.to_s