forked from AdaGold/dynamic-programming
-
Notifications
You must be signed in to change notification settings - Fork 53
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
Ruthie Newman, CS fundamentals, cohort 15, paper #49
Open
RuthieRNewman
wants to merge
6
commits into
Ada-C15:master
Choose a base branch
from
RuthieRNewman:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dc97e64
adds functionality to newman-conway
RuthieRNewman 7f86a34
feat: implement max_sub_array method.
RuthieRNewman 657a6c7
test: refactor max_sub_array unit tests to accomodate additional para…
RuthieRNewman 40f8fc0
feat: implement newman_conway method.
RuthieRNewman 5d44d2d
refactor: add default values for max_sub_array method parameters.
RuthieRNewman d119db0
test: revise max_sub_array unit tests to test functionality of refact…
RuthieRNewman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,60 @@ | ||
import sys | ||
|
||
def max_sub_array(nums): | ||
#To find the max sum of sub_array which includes values from left, mid and right of the nums array | ||
def max_mid_array(nums, left, mid, right): | ||
|
||
# To find the max sum of the left, iterate backwards | ||
#from the mid num | ||
sum = 0 | ||
left_sum = -sys.maxsize | ||
|
||
for i in range(mid, left-1, -1): | ||
sum = sum + nums[i] | ||
|
||
if (sum > left_sum): | ||
left_sum = sum | ||
|
||
#To find the max sum of right subarray iterate up(increasing index values)from the | ||
#num to the right of mid num(mid + 1). | ||
sum = 0 | ||
right_sum = -sys.maxsize | ||
|
||
for i in range(mid + 1, right + 1): | ||
sum = sum + nums[i] | ||
|
||
if (sum > right_sum): | ||
right_sum = sum | ||
|
||
return (left_sum + right_sum) | ||
|
||
#To find the max subarray | ||
def max_sub_array(nums, left=0, right=None): | ||
""" 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: Time complexity is O(nLogn) time because of the implementation of a Divide and Conquer approach, in which the max subarray of | ||
the given array is found through comparing the left and right value of a recursively calculated mid-point. This method also recursively calls the | ||
helper method; max_mid_array() which considers max subarrays that range from left to right across the mid-point, with an O(nLogn) time complexity. | ||
|
||
Space Complexity: Space complexity should be O(nLogn) as well because, according to my research, space complexity is relative to the size of the | ||
call stack, which decreases by 2 values with each recursive call. | ||
""" | ||
if nums == None: | ||
return 0 | ||
if len(nums) == 0: | ||
n = len(nums) | ||
|
||
if n == 0: | ||
return 0 | ||
pass | ||
|
||
if right == None: | ||
right = n-1 | ||
|
||
#base case == only one element | ||
if(left == right): | ||
return nums[left] | ||
|
||
mid = (left + right) // 2 | ||
|
||
maximum_sum_left_subarray = max_sub_array(nums, left, mid) | ||
maximum_sum_right_subarray = max_sub_array(nums, left+1, right) | ||
maximum_sum_crossing_subarray = max_mid_array(nums, left, mid, right); | ||
|
||
return max(maximum_sum_left_subarray, maximum_sum_right_subarray, maximum_sum_crossing_subarray) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,29 @@ | ||
|
||
|
||
# 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) because the number of calculations performed depends on the size of num. | ||
|
||
Space Complexity: Space complexity is also O(n) becuase newman_conway_nums array to store sequence values, | ||
nm_sequence_without_leading_zero to store result with leading 0 removed and result a array to which the properly | ||
formatted result is saved are created and the amount of space that they occupy will depend on the size of the given num. | ||
Comment on lines
2
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. 👍 |
||
""" | ||
pass | ||
if num == 0: | ||
raise ValueError | ||
|
||
if num == 1: | ||
return '1' | ||
|
||
if num == 2: | ||
return '1 1' | ||
|
||
#array to store sequence values and provide starting values | ||
newman_conway_nums = [0, 1, 1] | ||
|
||
for i in range(3, num + 1): | ||
newman_conway_nums.append(newman_conway_nums[newman_conway_nums[i-1]] + newman_conway_nums[i-newman_conway_nums[i-1]]) | ||
|
||
nm_sequence_without_leading_zero = [str(num) for num in newman_conway_nums if num != 0] | ||
result = " ".join(nm_sequence_without_leading_zero) | ||
|
||
return result | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 works, but if you use a dynamic programming approach you can get it done in O(n) time complexity.