-
Notifications
You must be signed in to change notification settings - Fork 51
Spruce - Angela F #38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,7 +4,24 @@ | |
| # 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
| if num == 0: | ||
| raise ValueError("Input must be greater than 0.") | ||
|
Comment on lines
+10
to
+11
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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("Input must be greater than 0.") |
||
|
|
||
| if num == 1: | ||
| return "1" | ||
|
|
||
| if num == 2: | ||
| return "1 1" | ||
|
|
||
| # Initialize the list of numbers. | ||
| numbers = [1, 1] | ||
|
|
||
| for i in range(2, num + 1): | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since later you cut off the last value, could you just range up to |
||
| numbers.append(numbers[numbers[i - 1] - 1] + numbers[i - numbers[i - 1]]) | ||
|
|
||
| # Return the list of numbers. | ||
| return " ".join(str(x) for x in numbers [0:num]) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 Another approach would be to make uses of the return " ".join(map(str, numbers))(this also assumes that you reduce the range calculation as indicated above). |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
✨ This looks good. A few cases could be combined to simplify things.
👀 What would the complexity of this be? 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.