Skip to content

Conversation

@fan-gela
Copy link

No description provided.

@anselrognlie anselrognlie self-requested a review July 18, 2022 21:16
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.

✨ 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.

🟡

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

Comment on lines +10 to +11
if num == 0:
raise ValueError("Input must be greater than 0.")

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

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

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

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.

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