-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathn_lines.py
More file actions
54 lines (45 loc) · 1.46 KB
/
n_lines.py
File metadata and controls
54 lines (45 loc) · 1.46 KB
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
import sys
"""
Write a program to read a multiple line text file and write the 'N' longest
lines to stdout. Where the file to be read is specified on the command line.
The first line contains the value of the number 'N' followed by multiple lines.
"""
def insert_line(lines, newline):
"""Insert the newline in the list of lines, according to the length of
the lines, from largest to smallest. Lines is already sorted by length"""
hi = len(lines)
lo = 0
while lo < hi:
mid = (lo+hi)//2
if len(lines[mid]) > len(newline): lo = mid+1
else: hi = mid
lines.insert(lo, newline)
def add_line(lines, newline, n):
"""
Assumes all previous lines are sorted. Checks if newline is longer than
shortest line, if it is, put it where it needs to be. If not, append to
the end, then check If length is longer than n, gets rid of shortest line.
"""
if len(newline) > len(lines[-1]):
insert_line(lines, newline)
else:
lines.append(newline)
if len(lines) > n:
del lines[-1]
test_cases = open(sys.argv[1], 'r')
lines = []
count = 0 # number of lines seen
for test in test_cases:
ntest = test.strip()
if not ntest == "":
if count == 0:
n = int(ntest)
count += 1
elif count == 1:
lines.append(ntest)
count += 1
else:
add_line(lines, ntest, n)
for i in lines:
print(i)
test_cases.close()