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
}
16 changes: 9 additions & 7 deletions lib/max_subarray.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@

def max_sub_array(nums):
""" Returns the max subarray of the given list of numbers.
Returns 0 if nums is None or an empty list.
Time Complexity: ?
Space Complexity: ?
Comment on lines -5 to -6

Choose a reason for hiding this comment

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

👀 Time and space complexity?

"""

if nums == None:
return 0
if len(nums) == 0:
return 0
pass

max_array = nums[0]
current_max = nums[0]

for i in range(1, len(nums)):
current_max = max(nums[i], current_max + nums[i])

Choose a reason for hiding this comment

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

✨ This is a really nice way to represent this calculation, which captures the underlying invariant that makes Kadane's algorithm work. This article has a fairly good explanation of why this is considered a dynamic programming approach (on the face it might not "feel" like one).

max_array = max(current_max, max_array)
return max_array
28 changes: 21 additions & 7 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,24 @@


# Time complexity: ?
# Space Complexity: ?
def newman_conway(num):
""" Returns a list of the Newman Conway numbers for the given value.
Time Complexity: ?
Space Complexity: ?
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.

👀 Time and space complexity?

"""
pass
if num == 0:
raise ValueError
Comment on lines +4 to +5

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

if num == 1:
return "1"

memo = [None] * (num + 1)

memo[0] = 0

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.

memo[1] = 1
memo[2] = 1

i = 3
while i <= 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, especially since you went to the trouble to pre-allocate your memo list.

memo[i] = memo[memo[i - 1]] + memo[i - memo[i - 1]]
i += 1

result = "" + str(memo[1])
for i in range(2, len(memo)):
result += " " + str(memo[i])
Comment on lines +21 to +22

Choose a reason for hiding this comment

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

👀 Repeated concatenation causes the O(n^2) time complexity (due to each concatenation needing to copy every previous concatenation).

Rather than repeated string concatenations, it's preferred to build up a list of values, and then join them all at the end. You already have a list of the numerical values, so we need only transform it into a list of strings, then join them. One way to accomplish this would be:

    return " ".join(map(str, memo[1:]))

which converts each of the numbers in memo (skipping the buffer value at position 0) to a string value in a new list, then joins those values separated by a space.


return result