-
Notifications
You must be signed in to change notification settings - Fork 47
Branches C. Gutierrez #37
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
base: master
Are you sure you want to change the base?
Conversation
CheezItMan
left a comment
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.
Overall nice work, you hit the learning goals here. Well done. Check my comments below especially with regard to time/space complexity. Let me know if you have questions.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def factorial(n) |
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.
This is not recursive 🤔
| return s | ||
| end | ||
|
|
||
| reversed_str = reverse(s[1..-1]) |
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.
s[1..-1] creates a new array and copies all the individual elements over and so is O(n) by itself.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def reverse(s) |
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.
- Recursion means you have some space complexity by the system stack.
- You are creating a new string with each recursive call so this is actually O(n2)
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def reverse_inplace(s) |
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.
- This is not in place
- This is doing the same as the above method.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def bunny(n) |
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.
👍 But you're not taking the stack into account for space complexity.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def nested(s) |
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.
👍 This works, but you have similar time/space issues with the above methods due to creating new arrays.
| elsif array[0] == value | ||
| return true | ||
| else | ||
| array.delete_at(0) |
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.
delete_at(0) makes all the elements shift over 1 index costing O(n) for just this one line.
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
|
|
||
| def search(array,value) |
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.
👍 This works, but you have similar time/space issues with the above methods due to deleting elements.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(1) | ||
| def is_palindrome(s) |
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.
👍 This works, but you have similar time/space issues with the above methods due to creating new arrays.
| # Time complexity: O(n + m) | ||
| # Space complexity: O(1) | ||
|
|
||
| def digit_match(n, m, c = 0) |
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.
👍 This works, but you have similar time issues with the above methods due to using chop.
You're also not considering the cost of the stack in space.
No description provided.