Skip to content

Commit

Permalink
refactor: move merge sorted array
Browse files Browse the repository at this point in the history
  • Loading branch information
BrianLusina committed Jan 16, 2024
1 parent ef5a46d commit 2a093aa
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
6 changes: 0 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
## Python Snippets

[![Build Status](https://travis-ci.org/BrianLusina/Python_Snippets.svg?branch=master)](https://travis-ci.org/BrianLusina/Python_Snippets)
[![codecov](https://codecov.io/gh/BrianLusina/Python_Snippets/branch/master/graph/badge.svg)](https://codecov.io/gh/BrianLusina/Python_Snippets)
[![Codacy Badge](https://api.codacy.com/project/badge/Grade/11cfc8e125c54bdb833fe19ed9ddad72)](https://www.codacy.com/app/BrianLusina/Python_Snippets?utm_source=github.com&utm_medium=referral&utm_content=BrianLusina/Python_Snippets&utm_campaign=Badge_Grade)
[![Codacy Badge](https://api.codacy.com/project/badge/Coverage/11cfc8e125c54bdb833fe19ed9ddad72)](https://www.codacy.com/app/BrianLusina/Python_Snippets?utm_source=github.com&utm_medium=referral&utm_content=BrianLusina/Python_Snippets&utm_campaign=Badge_Coverage)
[![CircleCI](https://circleci.com/gh/BrianLusina/Python_Snippets.svg?style=svg)](https://circleci.com/gh/BrianLusina/Python_Snippets)
[![Dependency Status](https://gemnasium.com/badges/github.com/BrianLusina/Python_Snippets.svg)](https://gemnasium.com/github.com/BrianLusina/Python_Snippets)
[![Dependency Status](https://dependencyci.com/github/BrianLusina/Python_Snippets/badge)](https://dependencyci.com/github/BrianLusina/Python_Snippets)

Repository for some of my simple [Python](https://www.python.org/ "Python") functions and snippets. Each directory
and/or python package has a readme for more information about the Python program
Expand Down
Empty file.
Original file line number Diff line number Diff line change
@@ -1,27 +1,44 @@
# Merge Sorted Array

You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing the number of elements in nums1 and nums2 respectively.
You are given two integer arrays nums1 and nums2, sorted in non-decreasing order, and two integers m and n, representing
the number of elements in nums1 and nums2 respectively.

Merge nums1 and nums2 into a single array sorted in non-decreasing order.

The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and the last n elements are set to 0 and should be ignored. nums2 has a length of n.
The final sorted array should not be returned by the function, but instead be stored inside the array nums1. To
accommodate this, nums1 has a length of m + n, where the first m elements denote the elements that should be merged, and
the last n elements are set to 0 and should be ignored. nums2 has a length of n.

```text
Example 1:
Input: nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3
Output: [1,2,2,3,5,6]
Explanation: The arrays we are merging are [1,2,3] and [2,5,6].
The result of the merge is [1,2,2,3,5,6] with the underlined elements coming from nums1.
```

```text
Example 2:
Input: nums1 = [1], m = 1, nums2 = [], n = 0
Output: [1]
Explanation: The arrays we are merging are [1] and [].
The result of the merge is [1].
```

```text
Example 3:
Input: nums1 = [0], m = 0, nums2 = [1], n = 1
Output: [1]
Explanation: The arrays we are merging are [] and [1].
The result of the merge is [1].
Note that because m = 0, there are no elements in nums1. The 0 is only there to ensure the merge result can fit in nums1.
```

## Related Topics

- Array
- Two Pointers
- Sorting
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
"""
Do not return anything, modify nums1 in-place instead.
Does not return anything, modifies nums1 in-place instead.
"""
while m > 0 and n > 0:
if nums1[m - 1] > nums2[n - 1]:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import unittest

from datastructures.arrays.merge_sorted_arrays import merge
from . import merge


class MergeSortedArraysTestCases(unittest.TestCase):
Expand All @@ -11,7 +11,7 @@ def test_should_return_0_when_empty_array(self):
self.assertEqual(nums1, [])

def test_set_nums1_to_1_2_2_3_5_6_when_nums1_is_1_2_3_0_0_0_and_m_is_3_and_nums2_is_2_5_6_and_n_is_3(
self,
self,
):
"""Should set nums1 to [1,2,2,3,5,6] when initially nums1 = [1,2,3,0,0,0], m = 3, nums2 = [2,5,6], n = 3"""
nums1 = [1, 2, 3, 0, 0, 0]
Expand Down

0 comments on commit 2a093aa

Please sign in to comment.