-
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
Branches - Michaela #28
base: master
Are you sure you want to change the base?
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,92 @@ | ||
# 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 "N must be greater than or equal to 0" if n < 0 | ||
return 1 if n <= 1 | ||
return n * factorial(n - 1) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def reverse(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. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
return "" if s.empty? | ||
return s[0] if s.length == 1 | ||
|
||
return reverse(s[1..-1]) + s[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.
|
||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def reverse_inplace(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. 👍 Brilliant, I love the helper! |
||
raise NotImplementedError, "Method not implemented" | ||
return helper_reverse(s) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
def helper_reverse(s, start_index = 0, finish_index = (s.length - 1)) | ||
return s if start_index >= finish_index | ||
temp = s[start_index] | ||
s[start_index] = s[finish_index] | ||
s[finish_index] = temp | ||
return helper_reverse(s, start_index + 1, finish_index - 1) | ||
end | ||
|
||
# Time complexity: O(log(n)) | ||
# Space complexity: O(log(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 n + bunny(n / 2.0 ) | ||
# This passes the tests, though technically it never hits 0 (just approaches 0) | ||
# An alternate solution is to return 1 + bunny(n - 0.5) but that would have a slower time complexity O(n) | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# 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. 👍 This works, but you have similar time/space issues with the |
||
raise NotImplementedError, "Method not implemented" | ||
return true if s.empty? | ||
return false if s.length.odd? | ||
|
||
return false if s[0] != "(" || s[-1] != ")" | ||
return nested(s[1...-1]) | ||
# To do this without creating new strings, make a helper method | ||
# that accepts additional parameters for left_char and right_char and increments | ||
# them inward each time, like I did for reverse_in_place with helper_reverse | ||
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. 👍 This works, but you have similar time/space issues with the above methods. |
||
raise NotImplementedError, "Method not implemented" | ||
return true if array[-1] == value | ||
return false if array.length <= 1 | ||
return search(array[0...-1], value) | ||
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.empty? | ||
return false if s[0] != s[-1] | ||
return is_palindrome(s[1...-1]) | ||
|
||
# To do this without creating new strings, make a helper method | ||
# that accepts additional parameters for left_char and right_char and increments | ||
# them inward each time, like I did for reverse_in_place with helper_reverse | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(log(n)) | ||
# Space complexity: O(log(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. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
if (n / 10) < 1 || (m / 10) < 1 | ||
if n % 10 != m % 10 | ||
return 0 | ||
else | ||
return 1 | ||
end | ||
end | ||
|
||
if n % 10 == m % 10 | ||
return 1 + digit_match((n / 10), (m / 10)) | ||
else | ||
return digit_match((n / 10), (m / 10)) | ||
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.
👍