-
-
Notifications
You must be signed in to change notification settings - Fork 46.6k
added a problem on kadane's algo and its solution. #8569
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
Conversation
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
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.
Better to place this in an existing directory rather than creating a new directory for it
@tianyizheng02 , there is no existing directory for this algo |
We don't make separate directories for each algorithm in this repo. Algorithms are grouped based on general categories such as dynamic programming, machine learning, sorting algorithms, etc. My point is that you should place your new algorithm file in the same directory as similar algorithms. |
ohh...will do it how to change the directory now?? |
…g/max_product_subarray.py
n = len(nums) | ||
|
||
if n == 0: | ||
return 0 |
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.
n = len(nums) | |
if n == 0: | |
return 0 | |
if not numbers: | |
return 0 |
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.
'n' is the length of nums list , so it will always be a number.
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.
We do not need it until line 23 so let's not run the len()
function before we need it.
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.
it's an edge case, so we need to check it first.. rather than going to next step.
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.
We can check it in two lines not three and without a function call.
max_till_now = nums[0] | ||
min_till_now = nums[0] | ||
max_prod = nums[0] |
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.
max_till_now = nums[0] | |
min_till_now = nums[0] | |
max_prod = nums[0] | |
max_till_now = min_till_now = max_prod = numbers[0] |
if nums[i] < 0: | ||
max_till_now, min_till_now = min_till_now, max_till_now | ||
max_till_now = max(nums[i], max_till_now * nums[i]) | ||
min_till_now = min(nums[i], min_till_now * nums[i]) |
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.
if nums[i] < 0: | |
max_till_now, min_till_now = min_till_now, max_till_now | |
max_till_now = max(nums[i], max_till_now * nums[i]) | |
min_till_now = min(nums[i], min_till_now * nums[i]) | |
number = numbers[i] | |
if number < 0: | |
max_till_now, min_till_now = min_till_now, max_till_now | |
max_till_now = max(number, max_till_now * number]) | |
min_till_now = min(number, min_till_now * number) |
Co-authored-by: Christian Clauss <[email protected]>
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
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.
Click here to look at the relevant links ⬇️
🔗 Relevant Links
Repository:
Python:
Automated review generated by algorithms-keeper. If there's any problem regarding this review, please open an issue about it.
algorithms-keeper
commands and options
algorithms-keeper actions can be triggered by commenting on this PR:
@algorithms-keeper review
to trigger the checks for only added pull request files@algorithms-keeper review-all
to trigger the checks for all the pull request files, including the modified files. As we cannot post review comments on lines not part of the diff, this command will post all the messages in one comment.NOTE: Commands are in beta and so this feature is restricted only to a member or owner of the organization.
if numbers is None or not isinstance(numbers, list): | ||
return 0 | ||
|
||
n = len(numbers) | ||
|
||
if n == 0: | ||
return 0 | ||
|
||
if not all(isinstance(x, int) for x in numbers): | ||
return 0 |
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.
It is the Pythonic thing to do to raise a TypeError if the caller passes in garbage data.
if numbers is None or not isinstance(numbers, list): | |
return 0 | |
n = len(numbers) | |
if n == 0: | |
return 0 | |
if not all(isinstance(x, int) for x in numbers): | |
return 0 | |
if not numbers: | |
return 0 | |
if not isinstance(numbers, (list, set, tuple)) or not all(isinstance(number, int) for number in numbers): | |
raise TypeError("numbers must be an iterable of integers") |
for more information, see https://pre-commit.ci
@cclauss , I have committed all your requests, but still the build is failing , why? |
On your local machine type |
here is the problem, this is returning float but int is needed. what should i return if there is float in numbers, print invalid??? |
Raise TypeError #8569 (comment) |
for more information, see https://pre-commit.ci
raised the type error, but still build is failing |
Co-authored-by: Christian Clauss <[email protected]>
for more information, see https://pre-commit.ci
Co-authored-by: Christian Clauss <[email protected]>
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.
Modified because max_product_subarray("ABC")
should not return 0
.
@cclauss , Thanks for the required modification. |
Thanks for your contribution. |
* added kadane's algorithm directory with one problem's solution. * added type hints * Rename kaadne_algorithm/max_product_subarray.py to dynamic_programming/max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py --------- Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
* added kadane's algorithm directory with one problem's solution. * added type hints * Rename kaadne_algorithm/max_product_subarray.py to dynamic_programming/max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update max_product_subarray.py * Update max_product_subarray.py * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Update dynamic_programming/max_product_subarray.py Co-authored-by: Christian Clauss <[email protected]> * Update max_product_subarray.py --------- Co-authored-by: Christian Clauss <[email protected]> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
Describe your change:
Checklist:
Fixes: #{$ISSUE_NO}
.