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

from json.encoder import INFINITY

Choose a reason for hiding this comment

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

👀 The INFINITY symbol is really just an alias for float('inf'), which you can access without needing to import anything from the json module.



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 +8 to +9

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

maximum = - INFINITY

Choose a reason for hiding this comment

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

Another approach would be to initialize maximum to some value actually found in the list, say nums[0], which we know must at least exist from the guard checks.

current_max = 0

for num in nums:

Choose a reason for hiding this comment

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

current_max += num
if current_max > maximum:
maximum = current_max
if current_max < 0:
current_max = 0
return maximum
30 changes: 27 additions & 3 deletions lib/newman_conway.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,31 @@
# 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! We do only make one pass over the desired number of terms during the calculation. The the join call also iterates through the string results, giving us O(2n) → O(n) for time. There is a cost we could try to account for in stringifying the numerical values (they get longer as the numbers get larger) but we can ignore that for the purposes of this exercise.

Storage-wise, we do need n items both in the dictionary and list, as well as the final returned string for O(3n) → O(n), again being a little hand-wavey with the overall size of the final string (and ignoring the sizes of the intermediate strings).


P(1) = 1
P(2) = 1
for all n > 2
P(n) = P(P(n - 1)) + P(n - P(n - 1))
"""
pass

# make a dict with key as n, and the value as P(n) (aka the output)

outputs = {}

res = []
Comment on lines +18 to +20

Choose a reason for hiding this comment

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

We don't really need two separate places to store the results. Since we are building up P(n) from smaller P() values, as long as we calculate the them in a strictly increasing order (as you do), we could store them in order in an array for later lookup. Using a dictionary can be more helpful if the repeated calculations happen in a less predictable order (often when we are using a recursion+memoization approach).

The other thing you're using the two representations for is to store the numerical result in one, and the string representation in the other. We could covert the numerical values to strings as part of joining them together by using map or a list comprehension.

Otherwise, great approach!

if num <= 0:
raise ValueError

for n in range(1, num + 1):
if n == 1 or n == 2:
outputs[n] = 1
res.append("1")
continue
# P(n -1) -> outputs[ n - 1]
value = outputs[outputs[n - 1]] + outputs[n - outputs[n - 1]]
outputs[n] = value
res.append(str(value))

return " ".join(res)

Choose a reason for hiding this comment

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

As mentioned above, we could store the numerical values in res for use during the calculation. But since the str join method only knows how to join together strings, we need to convert them from ints. We could accomplish this with methods resembling the following:

    return " ".join(map(str, res))

or

    return " ".join(str(num) for num in res)