-
Notifications
You must be signed in to change notification settings - Fork 51
Maple - Ruiz #37
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?
Maple - Ruiz #37
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.
The tests aren't passing for the Newman-Conway sequence problem (wave 1). Notice that we want to build a string with the whole sequence up to the requested term, not just calculate the final term. We should also so this in O(n) time complexity.
Please resubmit wave 01 with those requirements in mind. Let me know if you have any questions.
🔴
lib/newman_conway.py
Outdated
| Time Complexity: ? | ||
| Space Complexity: ? |
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 implementation is the recursive approach to the Newman-Conway series. This is similar to the recursive Fibonacci calculation discussed in the material, which we would like to avoid. From the project description
You should be able to do this in O(n) time complexity.
but this approach is not O(n). It could be made so using the primary dynamic programming approach: memoization, which can be done recursively, or iteratively.
Please try another approach to this one.
lib/newman_conway.py
Outdated
| Space Complexity: ? | ||
| """ | ||
| pass | ||
| if num == 1 or num == 2: |
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're missing a validation case here for the test that tries passing 0 as the num input. Currently, this results in a stack overflow.
lib/newman_conway.py
Outdated
| if num == 1 or num == 2: | ||
| return 1 | ||
| else: | ||
| return newman_conway(newman_conway(num - 1)) + newman_conway(num - newman_conway(num-1)) No newline at end of file |
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.
👀 Rather than making recursive calls (which balloon the time complexity fairly quickly for even small arguments), observe that later values in the sequence are determined by a combination of previous values in the sequence. Can we store the previous calculations to perform the later calculations more efficiently?
Also, notice that the problem description wants the whole sequence of values, not only the nth term in the sequence. Consider who this might imply using a list to store the intermediate values, since we'll need them at the end anyway.
| Time Complexity: On | ||
| Space Complexity: ON |
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.
👀 Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one).
For the space complexity, even though we are working with an input list of size n, that's not part of our space usage (it was passed in to us). The only space we use is a few numbers to calculate the running maximum (and iterate), so like the dynamic programming approach to calculating the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation. This lets us implement this with a constant O(1) amount of storage.
| max_sub_array = nums[0] | ||
| max_sub = nums[0] | ||
|
|
||
| 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.
Another way we could skip over the first value is to slice the array, or use islice from itertools to avoid making a copy (be sure to add from itertools import islice):
for num in islice(nums, 1, None):Then in the loop, replace nums[i] with num.
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.
✨ Nice job updating wave 01. I've adjusted this to a green.
🟢
| @@ -1,10 +1,32 @@ | |||
|
|
|||
|
|
|||
| import array | |||
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.
👀 Unused import
| 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 == 1: | ||
| return "1" | ||
|
|
||
| f = [0, 1, 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.
✨ Nice use of a buffer slot to account for the 1-based calculation.
| count = 3 | ||
| # To store values of sequence in array | ||
|
|
||
| while count <= 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.
👀 Prefer a for loop, since we know exactly how many times this will loop.
| number = [str(item) for item in f] | ||
|
|
||
| return " ".join(number[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.
✨ Nice use of a list comprehension to convert the numeric values to strings.
Another approach would be to use the map function:
return " ".join(map(str, number[1:]))
No description provided.