Conversation
CheezItMan
reviewed
Nov 14, 2019
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(n) | ||
| def factorial(n) |
| def reverse(s, pointer = s.length - 1) | ||
| return s if s == "" | ||
| return s[pointer] if pointer == 0 | ||
| return s[pointer] + reverse(s, pointer - 1) |
There was a problem hiding this comment.
String concatenation creates a new array and copies all the individual elements over and so is O(n) by itself.
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the string | ||
| # Space complexity: O(n), where n is the length of the string | ||
| def reverse(s, pointer = s.length - 1) |
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.
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the string. (More precisely, n/2) | ||
| # Space complexity: O(n), where n is the length of the string. (More precisely, n/2) | ||
| def reverse_inplace(s, start = 0, finish = s.length - 1) |
There was a problem hiding this comment.
- This is not in place.
- This is the same time/space complexity as the method above.
| # Space complexity: ? | ||
| # Time complexity: O(n) | ||
| # Space complexity: O(n) | ||
| def bunny(n) |
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the string | ||
| # Space complexity: O(n), where n is the length of the string | ||
| def nested(s, parens_counter = 0, pointer = 0) |
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the string | ||
| # Space complexity: O(n), where n is the length of the string | ||
| def search(array, value, pointer = 0) |
| def is_palindrome(s, length = s.length, pointer = 0) | ||
| return true if pointer > length / 2 | ||
| return false if s[pointer] != s[length - 1 - pointer] | ||
| return is_palindrome(s, length, pointer + 1) if s[pointer] == s[length - 1 - pointer] |
There was a problem hiding this comment.
You shouldn't need the if here.
Suggested change
| return is_palindrome(s, length, pointer + 1) if s[pointer] == s[length - 1 - pointer] | |
| return is_palindrome(s, length, pointer + 1) |
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the string (More precisely, n/2) | ||
| # Space complexity: O(n), where n is the length of the string (More precisely, n/2) | ||
| def is_palindrome(s, length = s.length, pointer = 0) |
| raise NotImplementedError, "Method not implemented" | ||
| # Time complexity: O(n), where n is the length of the shorter string | ||
| # Space complexity: O(n), where n is the length of the shorter string | ||
| def digit_match(n, m, pointer = -1, matches = 0) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Sorry this is a bit late--catching up from a weekend out of town. Recursion is fun :)