-
Notifications
You must be signed in to change notification settings - Fork 0
/
EPI_11_01.py
59 lines (42 loc) · 1.45 KB
/
EPI_11_01.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
# Merge sorted files
# Individual files are sorted in increasing order of time
# TODO: (BFan) Implement heap with tuples <overload comparison operator>
# Idea: Use Aux heap and an aux array
from Heap import *
def merge_sort(ls_arrays):
# heap to store the next first items from lists
min_heap = Heap(is_max=False)
# parallel array keep track of the next item
next_items = [0] * len(ls_arrays)
# push firsts on to heap
for ndx, ele in enumerate(ls_arrays):
if ele:
next_items[ndx] += 1
min_heap.insert(HeapNodes(ele[0], ndx))
res = []
while not min_heap.is_empty():
# pop the next one from the heap --> O(logn)
nxt = min_heap.pop_top()
res.append(nxt.data)
pos = nxt.origin
# if still more to push on to the heap, push the next one and move pointer forward
if next_items[pos] < len(ls_arrays[pos]):
next_to_insert = ls_arrays[pos][next_items[pos]]
min_heap.insert(HeapNodes(next_to_insert, pos))
next_items[pos] += 1
return res
if __name__ == '__main__':
test_cases = [
([[1, 2, 3], [2, 3, 4, 5, 6], [1, 2, 3, 4, 5]], [1, 1, 2, 2, 2, 3, 3, 3, 4, 4, 5, 5, 6]),
]
for case, exp in test_cases:
print merge_sort(case) == exp
'''
for files
'''
def open_multiple_files(ls_of_names):
try:
f = [open(file) for file in ls_of_names]
finally:
for fh in f:
fh.close()