-
Notifications
You must be signed in to change notification settings - Fork 47
Branches - Kelsey #23
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: also O(n), where a new method is stored n times in the call stack | ||
| 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.
👍
| if s.length > 1 | ||
| first_char = s[0] | ||
| last_char = s[-1] | ||
| s = reverse(s.delete_prefix(s[0]).delete_suffix(s[-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.
delete_prefix & delete_suffix create new arrays and copy all the individual elements over and so this line is O(n) by itself.
| # Space complexity: ? | ||
| # Time complexity: The method is called n/2 times, and removing the constant we get O(n) | ||
| # Space complexity: Since the method creates a new array every time it is called and stores a method in the call stack, it takes up 2n memory, but removing the constant makes the space complexity O(n) | ||
| 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.
👍
This works, but because you create a new array with each recursive call this is O(n2) for both time/space complexity.
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), similar to the reverse(s) method | ||
| # Space complexity: O(n), also similar to the reverse(s) method | ||
| def reverse_inplace(s, index = 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.
👍
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n), even though the method is storing a new "ears" value every time it is called | ||
| def bunny(n, ears = 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.
👍
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: The method will be called n/2 (or n/2 + 1) times, but since we remove the constant it's O(n) | ||
| # Space complexity: O(n) (same reasoning as above) | ||
| def nested(s, i = 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.
👍
| def search(array, value, i = 0) | ||
| if array[i] == value | ||
| return true | ||
| elsif i > array.length |
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.
| elsif i > array.length | |
| elsif i >= array.length |
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), similar to the nested method | ||
| # Space complexity: O(n), same | ||
| def is_palindrome(s, i = 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.
👍
| end | ||
| # Time complexity: O(n), where n is the number of digits in 'n' or 'm', whichever is smaller. | ||
| # Space complexity: O(n), same as above. | ||
| def digit_match(n, m, i = 0, result = 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.
👍
No description provided.