-
Notifications
You must be signed in to change notification settings - Fork 51
Spruce - Angela F #38
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
anselrognlie
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.
✨ Your implementations look good, Angela! I left some comments on your implementation below.
Because of the importance of thinking about complexity for this project, I've evaluated this as a yellow due to the missing complexities for the Largest Sum Contiguous Subarray problem (wave 02). A yellow is a passing score so resubmission is not required, but you are free to resubmit with that time and space complexity filled out for a green score.
🟡
| Time Complexity: O(n) | ||
| Space Complexity: O(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.
✨ Great! By carefully building up the calculations and storing them for later use, we only need to perform O(n) calculations. The storage to keep those calculations is related to n (as is the converted string) giving space complexity of O(n) as well (ignoring a little bit of fiddliness related to the length of larger numbers being longer strings).
| if num == 0: | ||
| raise ValueError("Input must be greater than 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.
We should raise this error for any value below the valid starting point of the sequence:
if num <= 0:
raise ValueError("Input must be greater than 0.")| # Initialize the list of numbers. | ||
| numbers = [1, 1] | ||
|
|
||
| for i in range(2, num + 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.
Since later you cut off the last value, could you just range up to num here?
| numbers.append(numbers[numbers[i - 1] - 1] + numbers[i - numbers[i - 1]]) | ||
|
|
||
| # Return the list of numbers. | ||
| return " ".join(str(x) for x in numbers [0:num]) |
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.
✨ Nice use of a generator to convert the numeric results to strings. This is a generator rather than a list comprehension because it lacks the [] around the comprehension expression. A generator produces a sequence of values (here, the stringified sequence values) and can be used anywhere an iterable value is needed.
Another approach would be to make uses of the map function
return " ".join(map(str, numbers))(this also assumes that you reduce the range calculation as indicated above).
| # Initialize the current_sum to the first element in the list. | ||
| current_sum = nums[0] | ||
| # Iterate through the list. | ||
| for i in range(1, len(nums)): |
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 looks good. A few cases could be combined to simplify things.
👀 What would the complexity of this be? How would this compare to a "naïve" approach? Though this might not look like what we would think of as a dynamic programming approach, this article has a fairly good explanation of why it is. The main reason we look for dynamic programming approaches is to significantly improve the time complexity of an otherwise nasty algorithm.
No description provided.