-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Angele Z. #32
base: master
Are you sure you want to change the base?
Angele Z. #32
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,87 @@ | ||
require 'pry' | ||
# 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(n) | ||
def reverse_inplace(s, i = 0) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
return s if i == s.length / 2 | ||
s[i], s[s.length - i - 1] = s[s.length - i - 1], s[i] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. clever |
||
s = reverse_inplace(s, i += 1) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def bunny(n) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
return 0 if n <= 0 | ||
return 2 + bunny(n - 1) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) since using .length? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
# Space complexity: O(n) | ||
def nested(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not working 😢 |
||
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] | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This means you're going from a string which could be 14 characters long immediately to a string of 2 characters... This won't work in all cases. |
||
end | ||
return nested(next_string) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def search(array, value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 |
||
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) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def is_palindrome(s) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This works, but you have similar time/space issues with the above methods. |
||
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: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) since it uses .length in each loop? | ||
# Space complexity: O(n) | ||
def digit_match(n, m) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 👍 This works, but you have similar time/space issues with the above methods. |
||
raise NotImplementedError, "Method not implemented" | ||
end | ||
n = n.to_s | ||
m = m.to_s | ||
return 0 if n == nil || m == nil || n.length == 0 || m.length == 0 | ||
match = n[-1] == m[-1] | ||
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) | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍