Skip to content

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

Merged
merged 23 commits into from
Mar 31, 2023

Conversation

rohan472000
Copy link
Contributor

@rohan472000 rohan472000 commented Mar 29, 2023

Describe your change:

  • Add an algorithm?
  • Fix a bug or typo in an existing algorithm?
  • Documentation change?

Checklist:

  • I have read CONTRIBUTING.md.
  • This pull request is all my own work -- I have not plagiarized.
  • I know that pull requests will not be merged if they fail the automated tests.
  • This PR only changes one algorithm file. To ease review, please open separate PRs for separate algorithms.
  • All new Python files are placed inside an existing directory.
  • All filenames are in all lowercase characters with no spaces or dashes.
  • All functions and variable names follow Python naming conventions.
  • All function parameters and return values are annotated with Python type hints.
  • All functions have doctests that pass the automated testing.
  • All new algorithms include at least one URL that points to Wikipedia or another similar explanation.
  • If this pull request resolves one or more open issues then the commit message contains Fixes: #{$ISSUE_NO}.

@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed require type hints https://docs.python.org/3/library/typing.html labels Mar 29, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a 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.

@rohan472000 rohan472000 changed the title added kadane's algorithm directory with one problem's solution. added kadane's algorithm directory with a problem and its solution. Mar 29, 2023
@algorithms-keeper algorithms-keeper bot removed the require type hints https://docs.python.org/3/library/typing.html label Mar 29, 2023
Copy link
Contributor

@tianyizheng02 tianyizheng02 left a 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

@rohan472000
Copy link
Contributor Author

@tianyizheng02 , there is no existing directory for this algo

@tianyizheng02
Copy link
Contributor

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.

@rohan472000
Copy link
Contributor Author

ohh...will do it

how to change the directory now??

@algorithms-keeper algorithms-keeper bot added the tests are failing Do not merge until tests pass label Mar 30, 2023
@rohan472000 rohan472000 changed the title added kadane's algorithm directory with a problem and its solution. added a problem on kadane's algo and its solution. Mar 30, 2023
Comment on lines 14 to 17
n = len(nums)

if n == 0:
return 0
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
n = len(nums)
if n == 0:
return 0
if not numbers:
return 0

Copy link
Contributor Author

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.

Copy link
Member

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.

Copy link
Contributor Author

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.

Copy link
Member

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.

Comment on lines 19 to 21
max_till_now = nums[0]
min_till_now = nums[0]
max_prod = nums[0]
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
max_till_now = nums[0]
min_till_now = nums[0]
max_prod = nums[0]
max_till_now = min_till_now = max_prod = numbers[0]

Comment on lines 25 to 28
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])
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
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)

@algorithms-keeper algorithms-keeper bot added awaiting changes A maintainer has requested changes to this PR and removed awaiting reviews This PR is ready to be reviewed labels Mar 30, 2023
@algorithms-keeper algorithms-keeper bot added awaiting reviews This PR is ready to be reviewed and removed awaiting changes A maintainer has requested changes to this PR labels Mar 30, 2023
Copy link

@algorithms-keeper algorithms-keeper bot left a 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.

Copy link

@algorithms-keeper algorithms-keeper bot left a 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.

Comment on lines 26 to 35
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
Copy link
Member

@cclauss cclauss Mar 30, 2023

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.

Suggested change
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")

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Mar 30, 2023
@rohan472000
Copy link
Contributor Author

@cclauss , I have committed all your requests, but still the build is failing , why?

@cclauss
Copy link
Member

cclauss commented Mar 30, 2023

On your local machine type python3 -m doctest -v dynamic_programming/max_product_subarray.py to see if the tests pass.

https://github.com/TheAlgorithms/Python/actions/runs/4561456657/jobs/8047431889?pr=8569#step:6:657

@rohan472000
Copy link
Contributor Author

max_product_subarray([2, 3, -2, 4.5, -1])
54.0

here is the problem, this is returning float but int is needed.

what should i return if there is float in numbers, print invalid???

@cclauss
Copy link
Member

cclauss commented Mar 30, 2023

Raise TypeError #8569 (comment)

@rohan472000
Copy link
Contributor Author

raised the type error, but still build is failing

@cclauss
Copy link
Member

cclauss commented Mar 30, 2023

Here is a doctest for an exception...

>>> ugly_numbers(-5.5)
Traceback (most recent call last):
...
TypeError: 'float' object cannot be interpreted as an integer

@algorithms-keeper algorithms-keeper bot removed the tests are failing Do not merge until tests pass label Mar 31, 2023
Copy link
Member

@cclauss cclauss left a 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.

@algorithms-keeper algorithms-keeper bot removed the awaiting reviews This PR is ready to be reviewed label Mar 31, 2023
@cclauss cclauss merged commit a004929 into TheAlgorithms:master Mar 31, 2023
@rohan472000
Copy link
Contributor Author

@cclauss , Thanks for the required modification.

@cclauss
Copy link
Member

cclauss commented Mar 31, 2023

Thanks for your contribution.

tianyizheng02 pushed a commit to tianyizheng02/Python that referenced this pull request May 29, 2023
* 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>
sedatguzelsemme pushed a commit to sedatguzelsemme/Python that referenced this pull request Sep 15, 2024
* 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>
@isidroas isidroas mentioned this pull request Jan 25, 2025
14 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants