-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBatchMerge.py
45 lines (33 loc) · 994 Bytes
/
BatchMerge.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
def merge(left, right):
if len(left) == 0:
return right
if len(right) == 0:
return left
result = []
index_left = index_right = 0
while len(result) < len(left) + len(right):
if left[index_left] <= right[index_right]:
result.append(left[index_left])
index_left += 1
else:
result.append(right[index_right])
index_right += 1
if index_right == len(right):
result += left[index_left:]
break
if index_left == len(left):
result += right[index_right:]
break
return result
partial1 = open('partial-1.txt')
partial2 = open('partial-2.txt')
sorted = open('sorted.txt', 'w')
list1 = []
list2 = []
for line in partial1:
list1.append(int(line.replace('\n', '')))
for line in partial2:
list2.append(int(line.replace('\n', '')))
mergedList = merge(list1, list2)
for number in mergedList:
sorted.write(str(number)+'\n')