-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.py
More file actions
74 lines (60 loc) · 2.1 KB
/
main.py
File metadata and controls
74 lines (60 loc) · 2.1 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import os
from typing import Generator, List
import sys
from record_utils import RecordUtils
def read_input_data(file_path=None) -> Generator[str, None, None]:
"""
Read input data from a file or stdin.
Args:
file_path (str): The path to the input file or None if reading from stdin.
Returns:
list: A generator yielding data lines.
"""
if file_path:
try:
with open(file_path, 'r') as file:
for line in file:
yield line
except Exception as e:
raise Exception(
f"Error occurred while reading file '{file_path}': {e}") from e
else:
print("Enter data in the format '<unique record identifier> <numeric value>', one record per line:")
while True:
line = input()
if not line:
break
yield line
def process_input_data(file_path=None):
"""
Process file content and find the unique IDs associated with the X-largest values in the rightmost column.
Args:
file_path (str): The path to the input file or None if reading from stdin.
"""
try:
if file_path and not os.path.exists(file_path):
print(f"File '{file_path}' not found.")
return
top_x = int(input("Enter the value of X: "))
if top_x <= 0:
print("Error: X must be a non-zero positive integer.")
return
data_lines = read_input_data(file_path)
top_x_ids = RecordUtils.find_largest_ids_parallel(data_lines, top_x)
print("Unique IDs of the X-largest values in the rightmost column:")
for record_id in top_x_ids:
print(record_id)
except ValueError:
print("Error: Invalid value of X, please enter a non-zero positive integer.")
except Exception as e:
print(f"Error: {e}")
if __name__ == "__main__":
if len(sys.argv) == 2:
file_path = sys.argv[1]
else:
file_path = None
try:
process_input_data(file_path)
except KeyboardInterrupt:
print("\nAborted by the user.")
sys.exit(0)