-
Notifications
You must be signed in to change notification settings - Fork 96
/
03.py
63 lines (51 loc) · 1.41 KB
/
03.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
from typing import List
def merge(nums1: List[int], m: int, nums2: List[int], n: int) -> None:
x = nums1[0:m]
y = nums2[0:n]
nums1=x+y
nums1 = merge_sort(nums1)
return nums1
def merge_sort(myList) -> None:
if len(myList) > 1:
mid = len(myList) // 2
left = myList[:mid]
right = myList[mid:]
# Recursive call on each half
merge_sort(left)
merge_sort(right)
# Two iterators for traversing the two halves
i = 0
j = 0
# Iterator for the main list
k = 0
while i < len(left) and j < len(right):
if left[i] <= right[j]:
# The value from the left half has been used
myList[k] = left[i]
# Move the iterator forward
i += 1
else:
myList[k] = right[j]
j += 1
# Move to the next slot
k += 1
# For all the remaining values
while i < len(left):
myList[k] = left[i]
i += 1
k += 1
while j < len(right):
myList[k]=right[j]
j += 1
k += 1
return myList
# Do not change the following code
nums1 = []
nums2 = []
for item in input().split(', '):
nums1.append(int(item))
for item in input().split(', '):
nums2.append(int(item))
m = int(input())
n = int(input())
print(merge(nums1, m, nums2, n))