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
}
12 changes: 11 additions & 1 deletion lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ def max_sub_array(nums):
"""
if nums == None:
return 0

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

max_til_now = nums[0]
max_ending = 0

for i in range(len(nums)):

Choose a reason for hiding this comment

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

👀 Implementation looks good, but what are the time and space complexity for this approach?

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.

max_ending += nums[i]
max_til_now = max(max_til_now, max_ending)
if(max_ending < 0):
max_ending = 0
return max_til_now
22 changes: 21 additions & 1 deletion lib/newman_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,25 @@ 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.

👀 Implementation looks good, but what are the time and space complexity for this approach?


P(n) = P(P(n - 1)) + P(n - P(n - 1))
"""
pass
# base = ['1','1']
if num == 0:
raise ValueError()
Comment on lines +13 to +14

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')
# for i in range(num):
# if i > 1:
# base.append(str(int(base[int(base[i - 1])]) + int(base[i - int(base[i - 1])])))

# return ' '.join(base)

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.

r = 1
# To store values of sequence in array
for i in range(3, num + 1):
r = f[f[i-1]]+f[i-f[i-1]]
f.append(r)

return ' '.join([str(j) for j in f[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, f[1:]))