-
Notifications
You must be signed in to change notification settings - Fork 3
/
MergeSortedArrays.py
66 lines (49 loc) · 1.48 KB
/
MergeSortedArrays.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# GHC Codepath - Module SE101
# Sandbox - 10
#!/bin/python3
import math
import os
import random
import re
import sys
# The function is expected to return an INTEGER_ARRAY.
# The function accepts following parameters:
# 1. INTEGER_ARRAY nums1
# 2. INTEGER_ARRAY nums2
# 3. INTEGER m
# 4. INTEGER n
# This function will merge nums1 and nums2 into 1
# sorted array. m and n indicated how many elements
# are initialized in nums1 and nums2 respectively.
def merge(nums1, nums2, m, n):
pointer_nums1 = 0
pointer_nums2 = 0
while pointer_nums2 < n:
if pointer_nums1 == len(nums1):
nums1 += nums2
break
if nums2[pointer_nums2] <= nums1[pointer_nums1] or nums1[pointer_nums1] == 0:
nums1.insert(pointer_nums1, nums2[pointer_nums2])
pointer_nums2 += 1
pointer_nums1 += 1
else:
pointer_nums1 += 1
return nums1[:m + n]
if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')
nums1_count = int(input().strip())
nums1 = []
for _ in range(nums1_count):
nums1_item = int(input().strip())
nums1.append(nums1_item)
nums2_count = int(input().strip())
nums2 = []
for _ in range(nums2_count):
nums2_item = int(input().strip())
nums2.append(nums2_item)
m = int(input().strip())
n = int(input().strip())
result = merge(nums1, nums2, m, n)
fptr.write('\n'.join(map(str, result)))
fptr.write('\n')
fptr.close()