-
-
Notifications
You must be signed in to change notification settings - Fork 51.1k
Add Merge Sorted Arrays Algorithm with Type Hints and Doctests #13589
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
+77
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
a59bc60
Add merge sorted array algorithm
JainRamyak f14774b
Merge branch 'master' into rj1
JainRamyak 97b3c33
updating DIRECTORY.md
JainRamyak f808867
Merge branch 'master' into rj1
cclauss 743becc
Validate input arrays for sorting in merge function
cclauss ea2a6ea
Correct error message assignment in merge_sorted_arrays
cclauss c464181
Fix sorted check for input arrays in merge function
cclauss 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 hidden or 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
This file contains hidden or 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,76 @@ | ||
| def merge_sorted_arrays(nums1: list[int], nums2: list[int]) -> list[int]: | ||
| """ | ||
| Merge two sorted arrays into one sorted array. | ||
|
|
||
| Args: | ||
| nums1: The first sorted array. | ||
| nums2: The second sorted array. | ||
|
|
||
| Returns: | ||
| A single merged and sorted array. | ||
|
|
||
| Examples: | ||
| >>> merge_sorted_arrays([1, 3, 5], [2, 4, 6]) | ||
| [1, 2, 3, 4, 5, 6] | ||
|
|
||
| >>> merge_sorted_arrays([1, 2], []) | ||
| [1, 2] | ||
|
|
||
| >>> merge_sorted_arrays([], [3, 4]) | ||
| [3, 4] | ||
|
|
||
| >>> merge_sorted_arrays([], []) | ||
| [] | ||
|
|
||
| >>> merge_sorted_arrays([0, 0], [0, 0]) | ||
| [0, 0, 0, 0] | ||
|
|
||
| >>> merge_sorted_arrays([-5, -3, -1], [-2, -2]) | ||
| [-5, -3, -2, -2, -1] | ||
|
|
||
| >>> merge_sorted_arrays(range(5), range(5)) | ||
| [0, 0, 1, 1, 2, 2, 3, 3, 4, 4] | ||
|
|
||
| >>> merge_sorted_arrays([1, -1], []) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: nums = [1, -1] is not sorted | ||
|
|
||
| >>> merge_sorted_arrays([], [1, -1]) | ||
| Traceback (most recent call last): | ||
| ... | ||
| ValueError: nums = [1, -1] is not sorted | ||
| """ | ||
| for nums in (nums1, nums2): | ||
| if list(nums) != sorted(nums): | ||
| msg = f"{nums = } is not sorted" | ||
| raise ValueError(msg) | ||
| # If one array is empty, simply return the other. | ||
| if not nums1: | ||
| return nums2 | ||
| if not nums2: | ||
| return nums1 | ||
|
|
||
| # Two-pointer approach to merge both sorted arrays. | ||
| i, j = 0, 0 | ||
| merged = [] | ||
|
|
||
| while i < len(nums1) and j < len(nums2): | ||
| if nums1[i] <= nums2[j]: | ||
| merged.append(nums1[i]) | ||
| i += 1 | ||
| else: | ||
| merged.append(nums2[j]) | ||
| j += 1 | ||
|
|
||
| # Append remaining elements if any. | ||
| merged.extend(nums1[i:]) | ||
| merged.extend(nums2[j:]) | ||
|
|
||
| return merged | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
|
|
||
| doctest.testmod() | ||
Oops, something went wrong.
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.
An error occurred while parsing the file:
data_structures/arrays/merge_sorted.py