-
Notifications
You must be signed in to change notification settings - Fork 53
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
Earth - Lina #28
base: master
Are you sure you want to change the base?
Earth - Lina #28
Changes from 4 commits
48f1be8
357ab89
28b9c24
c82dfd8
db0a104
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,45 +1,86 @@ | ||
# 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 if n < 0 | ||
|
||
if n == 0 #base case | ||
return 1 | ||
else | ||
return n * factorial(n-1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
def reverse(s) | ||
Comment on lines
+16
to
18
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 s.length <= 1 | ||
return s | ||
else | ||
return s[-1] + reverse(s[1..-2]) + s[0] | ||
end | ||
end | ||
|
||
|
||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
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. Think about adjusting the method signature to this. def reverse_inplace(s, low = 0, high = s.length - 1) Does this help you see a way to do it in-place. |
||
raise NotImplementedError, "Method not implemented" | ||
if s.length <= 1 | ||
return s | ||
else | ||
return reverse_inplace(s[1..-1]) + s[0] | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n) | ||
# Space complexity: O(n) | ||
def bunny(n) | ||
Comment on lines
+49
to
51
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 == 0 | ||
return 0 | ||
else | ||
return 2 + bunny(n-1) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
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. 👍 , however can you see a way to do this O(n) in time/space? |
||
raise NotImplementedError, "Method not implemented" | ||
if s.empty? | ||
return true | ||
elsif s[0] == s[-1] | ||
return false | ||
else | ||
return nested(s[1..-2]) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
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" | ||
if array.empty? | ||
return false | ||
elsif array[0] == value | ||
return true | ||
else | ||
return search(array[1..-1], value) | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
# Space complexity: ? | ||
# Time complexity: O(n^2) | ||
# Space complexity: O(n^2) | ||
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. 👍 |
||
raise NotImplementedError, "Method not implemented" | ||
if s.length == 1 || s.empty? | ||
return true | ||
end | ||
|
||
if s[0] == s[-1] | ||
is_palindrome(s[1..-2]) | ||
else | ||
return false | ||
end | ||
end | ||
|
||
# Time complexity: ? | ||
|
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.
👍