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: 9 additions & 3 deletions lib/max_subarray.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,17 @@
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: ?
Time Complexity: O(n)
Space Complexity: O(1)
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.

✨ Notice how better time complexity this approach achieves over a "naïve" approach of checking for the maximum achievable sum starting from every position and every length. The correctness of this approach might not be apparent, so I definitely encourage reading a bit more about it. This has a fairly good explanation, as well as a description of why this is considered a dynamic programming approach (on the face it might not "feel" like one).

Since like the fibonacci sequence, we are able to maintain a sliding window of recent values to complete our calculation, we can do it with a constant O(1) amount of storage.


"""
if nums == None:
return 0
if len(nums) == 0:
return 0
pass
maxSub = nums[0]
currentSum = nums[0]
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.

👀 Prefer snake case

for index in range(1, len(nums)):
currentSum = max(nums[index], currentSum + nums[index])

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.

maxSub = max(maxSub, currentSum)
return maxSub
43 changes: 36 additions & 7 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,39 @@


# Time complexity: ?
# 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 +3 to +4

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
# P(1) = 1
# P(2) = 1
# for all n > 2
# P(n) = P(P(n - 1)) + P(n - P(n - 1))
# use memoization
if num == 0:
raise ValueError()
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()

if num == 1:
return "1"
if num == 2:
return "1 1"
new_seq = [0] * (num + 1)
new_seq[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.

new_seq[1] = 1
result = []
if num > 2:
new_seq[2] = 1

i = 3
# first 3 items
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 through the trouble to pre-allocate the array storage).

new_seq[i]
# P(n) = P(P(n - 1)) + P(n - P(n - 1))
new_seq[i] = new_seq[new_seq[i-1]] + new_seq[i- new_seq[i-1]]
i +=1

i = 1
result = []
while (i <= num) :
# Display the sequence element
result.append(new_seq[i])
i += 1
Comment on lines +32 to +37

Choose a reason for hiding this comment

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

This code is making a new array with that buffer 0 excluded. Remember that we could accomplish this with slicing.

    result = new_seq[1:]

resultOutput = [str(item) for item in result]

Choose a reason for hiding this comment

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

👀 Prefer snake case

return " ".join(resultOutput)
Comment on lines +38 to +39

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, result))