Skip to content

Conversation

@Jruiz9312
Copy link

No description provided.

@anselrognlie anselrognlie self-requested a review July 18, 2022 15:37
Copy link

@anselrognlie anselrognlie left a 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.

🔴

Comment on lines 7 to 8
Time Complexity: ?
Space Complexity: ?

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.

Space Complexity: ?
"""
pass
if num == 1 or num == 2:

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.

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

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.

Comment on lines +5 to +6
Time Complexity: On
Space Complexity: ON

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)):

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.

Copy link

@anselrognlie anselrognlie left a 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👀 Unused import

Comment on lines +7 to +8
Time Complexity: o(n)
Space Complexity: o(n)

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]

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:

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.

Comment on lines +25 to +27
number = [str(item) for item in f]

return " ".join(number[1:])

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:]))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants