-
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
India - Water #36
base: master
Are you sure you want to change the base?
India - Water #36
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,27 +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" | ||||||||||||||
if n == 0 | ||||||||||||||
return 1 | ||||||||||||||
elsif n < 0 | ||||||||||||||
raise ArgumentError | ||||||||||||||
else | ||||||||||||||
return n * factorial(n-1) | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
puts factorial(5) | ||||||||||||||
|
||||||||||||||
# Time complexity: ? | ||||||||||||||
# Space complexity: ? | ||||||||||||||
def reverse(s) | ||||||||||||||
Comment on lines
17
to
19
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. 👍 Time & space complexity? |
||||||||||||||
raise NotImplementedError, "Method not implemented" | ||||||||||||||
if s.length <=1 | ||||||||||||||
return s | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
return s[-1] + reverse(s[1..-2]) + s[0] | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# Time complexity: ? | ||||||||||||||
# Space complexity: ? | ||||||||||||||
# Time complexity: O(n^2) | ||||||||||||||
# Space complexity: O(n^2) | ||||||||||||||
def reverse_inplace(s) | ||||||||||||||
Comment on lines
+27
to
29
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 method is not reversing the string in place, instead it's creating a new string with each recursive call. Could you see a way to do it with the following method signature?
Suggested change
|
||||||||||||||
raise NotImplementedError, "Method not implemented" | ||||||||||||||
if s.length <=1 | ||||||||||||||
return s | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
new_s = reverse_inplace(s[1..-2]) | ||||||||||||||
s[1..-2] = new_s | ||||||||||||||
s[0] , s[-1] = s[-1], s[0] | ||||||||||||||
return s | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# Time complexity: ? | ||||||||||||||
# Space complexity: ? | ||||||||||||||
puts reverse_inplace("hello world") | ||||||||||||||
|
||||||||||||||
# Time complexity: O(n) | ||||||||||||||
# Space complexity: O(n) | ||||||||||||||
def bunny(n) | ||||||||||||||
Comment on lines
+42
to
44
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: ? | ||||||||||||||
|
@@ -30,16 +52,28 @@ def nested(s) | |||||||||||||
raise NotImplementedError, "Method not implemented" | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
# Time complexity: ? | ||||||||||||||
# Space complexity: ? | ||||||||||||||
# Time complexity: O(n^2) | ||||||||||||||
# Space complexity: O(n^2) | ||||||||||||||
def search(array, value) | ||||||||||||||
Comment on lines
+55
to
57
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 how to do this method with O(n) space/time complexity? |
||||||||||||||
raise NotImplementedError, "Method not implemented" | ||||||||||||||
if array[0] == value | ||||||||||||||
return true | ||||||||||||||
elsif array.length < 1 | ||||||||||||||
return false | ||||||||||||||
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) | ||||||||||||||
Comment on lines
+67
to
69
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. 👍 Similar note to the above on time/space complexity. |
||||||||||||||
raise NotImplementedError, "Method not implemented" | ||||||||||||||
if s.length <= 1 | ||||||||||||||
return true | ||||||||||||||
elsif s[0] != s[-1] | ||||||||||||||
return false | ||||||||||||||
else | ||||||||||||||
return is_palindrome(s[1..-2]) | ||||||||||||||
end | ||||||||||||||
end | ||||||||||||||
|
||||||||||||||
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. Just noting the incomplete methods. |
||||||||||||||
# 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.
👍