From 7034c2bd90b262eba118dc724dafb1a6510c4b23 Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Sat, 9 Nov 2019 19:32:32 -0800 Subject: [PATCH 1/4] First five methods created --- lib/recursive-methods.rb | 52 +++++++++++++++++++++++++++++++++------- 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index fbf6faa..1b3ee9a 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -3,31 +3,67 @@ # Time complexity: ? # Space complexity: ? def factorial(n) - raise NotImplementedError, "Method not implemented" + if n < 0 + raise ArgumentError + end + if n <= 1 + return 1 + else + n * factorial(n - 1) + end end # Time complexity: ? # Space complexity: ? def reverse(s) - raise NotImplementedError, "Method not implemented" + if s.length > 1 + first_char = s[0] + last_char = s[-1] + s = reverse(s.delete_prefix(s[0]).delete_suffix(s[-1])) + return last_char + s + first_char + else + return s + end end # Time complexity: ? # Space complexity: ? -def reverse_inplace(s) - raise NotImplementedError, "Method not implemented" +def reverse_inplace(s, index = 1) + if index <= s.length/2 + first_char = s[index-1] + last_char = s[-index] + s[index-1] = last_char + s[-index] = first_char + return reverse_inplace(s, index += 1) + else + return s + end end # Time complexity: ? # Space complexity: ? -def bunny(n) - raise NotImplementedError, "Method not implemented" +def bunny(n, ears = 0) + if ears == n + n + return ears + else + ears += 2 + return bunny(n, ears) + end end # Time complexity: ? # Space complexity: ? -def nested(s) - raise NotImplementedError, "Method not implemented" +def nested(s, i = 1) + if s.length % 2 != 0 + return false + end + if i > s.length / 2 || s.length == 0 + return true + elsif s[i-1] == "(" && s[-i] == ")" + return nested(s, i += 1) + else + return false + end end # Time complexity: ? From cf45f2efdc2825ac2b6b6a6860532a05698f8c99 Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Sun, 10 Nov 2019 15:16:54 -0800 Subject: [PATCH 2/4] Time & Space complexities added for functioning methods --- lib/recursive-methods.rb | 30 ++++++++++++++++++------------ 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 1b3ee9a..51a6324 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -1,7 +1,7 @@ # Authoring recursive algorithms. Add comments including time and space complexity for each method. -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: also O(n), where a new method is stored n times in the call stack def factorial(n) if n < 0 raise ArgumentError @@ -13,8 +13,8 @@ def factorial(n) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: The method is called n/2 times, and removing the constant we get O(n) +# Space complexity: Since the method creates a new array every time it is called and stores a method in the call stack, it takes up 2n memory, but removing the constant makes the space complexity O(n) def reverse(s) if s.length > 1 first_char = s[0] @@ -40,8 +40,8 @@ def reverse_inplace(s, index = 1) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n) +# Space complexity: O(n), even though the method is storing a new "ears" value every time it is called def bunny(n, ears = 0) if ears == n + n return ears @@ -51,8 +51,8 @@ def bunny(n, ears = 0) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: The method will be called n/2 (or n/2 + 1) times, but since we remove the constant it's O(n) +# Space complexity: O(n) (same reasoning as above) def nested(s, i = 1) if s.length % 2 != 0 return false @@ -66,10 +66,16 @@ def nested(s, i = 1) end end -# Time complexity: ? -# Space complexity: ? -def search(array, value) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n), where n is the array length +# Space complexity: O(n), where n is the array length +def search(array, value, i = 0) + if array[i] == value + return true + elsif i > array.length + return false + else + return search(array, value, i += 1) + end end # Time complexity: ? From 3f88ef9c44a1514e911d616be9a02371935aa956 Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Sun, 10 Nov 2019 22:47:34 -0800 Subject: [PATCH 3/4] All methods passing --- lib/recursive-methods.rb | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 51a6324..404b713 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -78,14 +78,31 @@ def search(array, value, i = 0) end end -# Time complexity: ? -# Space complexity: ? -def is_palindrome(s) - raise NotImplementedError, "Method not implemented" +# Time complexity: O(n), similar to the nested method +# Space complexity: O(n), same +def is_palindrome(s, i = 1) + if s[i-1] == s[-i] && i <= s.length/2 + is_palindrome(s, i += 1) + elsif i > s.length/2 + return true + else + return false + end 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, result = 0) + dec = 10**i + if n == 0 && m == 0 + return 1 + end + if n - dec >= 0 && m - dec >= 0 + if n/dec % 10 == m/dec % 10 + result += 1 + end + digit_match(n, m, i += 1, result) + else + return result + end end \ No newline at end of file From 80c32cfd5640e4f412a4c7b95ed210c6c90f4e46 Mon Sep 17 00:00:00 2001 From: Kelsey Krippaehne Date: Mon, 11 Nov 2019 19:17:13 -0800 Subject: [PATCH 4/4] Final time & space complexities added --- lib/recursive-methods.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/recursive-methods.rb b/lib/recursive-methods.rb index 404b713..38cafd7 100644 --- a/lib/recursive-methods.rb +++ b/lib/recursive-methods.rb @@ -26,8 +26,8 @@ def reverse(s) end end -# Time complexity: ? -# Space complexity: ? +# Time complexity: O(n), similar to the reverse(s) method +# Space complexity: O(n), also similar to the reverse(s) method def reverse_inplace(s, index = 1) if index <= s.length/2 first_char = s[index-1] @@ -90,19 +90,19 @@ def is_palindrome(s, i = 1) end end -# Time complexity: O(n) -# Space complexity: O(n) +# Time complexity: O(n), where n is the number of digits in 'n' or 'm', whichever is smaller. +# Space complexity: O(n), same as above. def digit_match(n, m, i = 0, result = 0) - dec = 10**i + place = 10**i if n == 0 && m == 0 return 1 end - if n - dec >= 0 && m - dec >= 0 - if n/dec % 10 == m/dec % 10 + if n - place >= 0 && m - place >= 0 + if n/place % 10 == m/place % 10 result += 1 end digit_match(n, m, i += 1, result) else return result end -end \ No newline at end of file +end