Conversation
CheezItMan
left a comment
There was a problem hiding this comment.
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) |
| return s | ||
| end | ||
|
|
||
| reversed_str = reverse(s[1..-1]) |
There was a problem hiding this comment.
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.
- 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.
- 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.
👍 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.
👍 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.
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.
👍 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.
👍 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.
👍 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.