Skip to content

Patch 5 #9378

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

Closed
wants to merge 16 commits into from
Closed

Patch 5 #9378

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
30 changes: 30 additions & 0 deletions data_structures/arrays/kadanes_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"""Given an integer array nums, find the subarray with the largest sum, and return its sum.
Example 1:
Input: nums = [-2,1,-3,4,-1,2,1,-5,4]
Output: 6
Explanation: The subarray [4,-1,2,1] has the largest sum 6.
Example 2:
Input: nums = [1]
Output: 1
Explanation: The subarray [1] has the largest sum 1.
Example 3:
Input: nums = [5,4,-1,7,8]
Output: 23
Explanation: The subarray [5,4,-1,7,8] has the largest sum 23.
"""


def max_subArray(self, nums: List[int]) -> int:

Choose a reason for hiding this comment

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

Variable and function names should follow the snake_case naming convention. Please update the following name accordingly: max_subArray

As there is no test file in this pull request nor any test function or class in the file data_structures/arrays/kadanes_algorithm.py, please provide doctest for the function max_subArray

# kadane's algorithm#
curr = 0
maxtillnow = -inf
for c in nums:
curr = max(c, curr + c)
maxtillnow = max(curr, maxtillnow)
return maxtillnow


if __name__ == "__main__":
import doctest

doctest.testmod()
47 changes: 47 additions & 0 deletions other/two_sum_advance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
"""<----------------------------------------2 POINTER SOLUTION --------------------------------------->

Given a 1-indexed array of integers numbers that is already sorted in non-decreasing order, find two numbers such that they add up to a specific target number. Let these two numbers be numbers[index1] and numbers[index2] where 1 <= index1 < index2 < numbers.length.
Return the indices of the two numbers, index1 and index2, added by one as an integer array [index1, index2] of length 2.
The tests are generated such that there is exactly one solution. You may not use the same element twice.
Your solution must use only constant extra space.
Example 1:

Input: numbers = [2,7,11,15], target = 9
Output: [1,2]
Explanation: The sum of 2 and 7 is 9. Therefore, index1 = 1, index2 = 2. We return [1, 2].
Example 2:

Input: numbers = [2,3,4], target = 6
Output: [1,3]
Explanation: The sum of 2 and 4 is 6. Therefore index1 = 1, index2 = 3. We return [1, 3].
Example 3:

Input: numbers = [-1,0], target = -1
Output: [1,2]
Explanation: The sum of -1 and 0 is -1. Therefore index1 = 1, index2 = 2. We return [1, 2].


#-------------------------CODE----------------# """


def two_sum(self, numbers: List[int], target: int) -> List[int]:

Choose a reason for hiding this comment

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

As there is no test file in this pull request nor any test function or class in the file other/two_sum_advance.py, please provide doctest for the function two_sum

n = len(numbers)
l = 0
r = n - 1
res = []
while l < r:
if numbers[l] + numbers[r] == target:
res.append(l + 1)
res.append(r + 1)
if numbers[l] + numbers[r] > target:
r -= 1
else:
l += 1
res.sort()
return res


if __name__ == "__main__":
import doctest

doctest.testmod()
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ pandas
pillow
projectq
qiskit
qiskit-aer
requests
rich
scikit-fuzzy
Expand Down