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

import math

Choose a reason for hiding this comment

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

👀 Not used

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.
Expand All @@ -9,4 +9,16 @@ def max_sub_array(nums):
return 0
if len(nums) == 0:
return 0
pass
largest_sum = [0] * len(nums)
largest_sum[0] = 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.

✨ Nice approach that really shows why Kadane's algorithm is really a form of dynamic programming.

👀 What's the time and space complexity of this approach? Could we make an adjustment to who we're carrying along the previous results to improve the space complexity a bit more?

if largest_sum[i - 1] <= 0:
largest_sum[i] = nums[i]

else:
largest_sum[i] = nums[i] + largest_sum[i-1]

return max(largest_sum)


26 changes: 21 additions & 5 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,26 @@


# Time complexity: ?
# Space Complexity: ?
# Time complexity: o(n)
# Space Complexity: o(n)
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
nc_list = [0] * (num + 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.

if num == 0:
raise ValueError("n must be > 0")
Comment on lines +11 to +12

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("n must be > 0")

if num == 1:
return "1"
if num == 2:
return "1 1"
if num > 2:
nc_list[1] = 1
nc_list[2] = 1
for i in range(3, num + 1):

nc_list[i] = nc_list[nc_list[i-1]] + nc_list[i - nc_list[i-1]]

return " ".join(str(v) for v in nc_list[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 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, nc_list[1:]))

print(newman_conway(1))