Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"python.testing.pytestArgs": [
"tests"
],
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true
}
26 changes: 25 additions & 1 deletion lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,28 @@ def max_sub_array(nums):
return 0
if len(nums) == 0:
return 0
pass
if len(nums) == 1:
return nums[0]

# Initialize the max_sum to the first element in the list.
max_sum = nums[0]
# 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.

# If the current sum is greater than 0, add the next element to it.
if current_sum > 0:
current_sum += nums[i]
# If the current sum is less than 0, set it to the next element.
elif current_sum < 0:
current_sum = nums[i]
# If the current sum is 0, set it to the next element.
elif current_sum == 0:
current_sum = nums[i]
# If the current sum is greater than the max sum, set the max sum to it.
if current_sum > max_sum:
max_sum = current_sum
# Return the max sum.
return max_sum


23 changes: 20 additions & 3 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
# Space Complexity: ?
def newman_conway(num):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: ?
Space Complexity: ?
Time Complexity: O(n)
Space Complexity: O(n)
Comment on lines +7 to +8

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

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

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


if num == 1:
return "1"

if num == 2:
return "1 1"

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