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. Also a small bug in digit_match. Let me know if you have questions.
| @@ -0,0 +1,46 @@ | |||
| def binary_search(array, to_find) | |||
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def factorial(n) |
| def reverse(s) | ||
| raise NotImplementedError, "Method not implemented" | ||
| return s if s.length == 1 || s.empty? | ||
| return s[-1] + reverse(s[1..-2]) + s[0] |
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(n) | ||
| def reverse(s) |
There was a problem hiding this comment.
👍
This works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) |
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def nested(s) |
There was a problem hiding this comment.
👍 This works, but you have similar time/space issues with the above methods.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def search(array, value) |
There was a problem hiding this comment.
👍 This works, but you have similar time/space issues with the above methods.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def is_palindrome(s) |
There was a problem hiding this comment.
👍 This works, but you have similar time/space issues with the above methods.
| return 1 if n == 0 && m == 0 | ||
| return 0 if n == 0 || m == 0 | ||
| return 1 if n.digits[0] == 0 && m.digits[0] == 0 | ||
| return 0 + digit_match(n / 10, m / 10) if n % 10 != m % 10 | ||
| return 1 + digit_match(n / 10, m / 10) if n % 10 == m % 10 |
There was a problem hiding this comment.
This will fail for n = 20 and m = 20.
| # Space complexity: ? | ||
| # Time complexity: O(n) if I had gotten it to work | ||
| # Space complexity: O(n) | ||
| def reverse_inplace(s) |
There was a problem hiding this comment.
- This method is not in place because
s[1..-2]creates a new array. - It's not working.
consider instead:
def reverse_in_place(s, left = 0, right = s.length -1)
if left < right
temp = s[left]
s[left] = s[right]
s[right] = temp
return reverse_in_place(s, left + 1, right - 1)
end
return s
end
No description provided.