-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscale_down.py
31 lines (25 loc) · 1.06 KB
/
scale_down.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
import sys
from collections import defaultdict
def scale_down(input_filename, output_filename, divisor):
# Create a dictionary to store and aggregate values based on the scaled key
scaled_values = defaultdict(int)
# Read the input file and process each line
with open(input_filename, 'r') as input_file:
for line in input_file:
parts = line.split()
if len(parts) == 2:
key, value = int(parts[0]), int(parts[1])
scaled_key = (key // divisor) + 1
scaled_values[scaled_key] += value
# Write the aggregated results to the output file
with open(output_filename, 'w') as output_file:
for key, value in sorted(scaled_values.items()):
output_file.write(f"{key} {value}\n")
if __name__ == "__main__":
if len(sys.argv) != 4:
print("Usage: python3 scale_down.py <input_file> <output_file> <divisor>")
sys.exit(1)
input_file = sys.argv[1]
output_file = sys.argv[2]
divisor = int(sys.argv[3])
scale_down(input_file, output_file, divisor)