csci5451/assignments/02/process_output.py

27 lines
587 B
Python
Raw Normal View History

2023-10-30 09:09:03 +00:00
import sys
import re
2023-10-31 04:37:41 +00:00
pat = re.compile(r"\[(\d+),(-?\d+)\] (.*)")
2023-10-30 09:09:03 +00:00
outputs = {}
2023-10-31 04:37:41 +00:00
for i, line in enumerate(sys.stdin.readlines()):
2023-10-30 09:09:03 +00:00
m = pat.match(line)
if not m:
# print(line)
continue
p = int(m.group(1))
2023-10-31 04:37:41 +00:00
n = int(m.group(2))
rest = m.group(3)
2023-10-30 09:09:03 +00:00
2023-10-31 04:37:41 +00:00
if (p, n) not in outputs: outputs[p, n] = (i, [])
outputs[p, n][1].append(rest)
2023-10-30 09:09:03 +00:00
2023-10-31 04:37:41 +00:00
for ((p, n), (i, lines)) in sorted(outputs.items(), key=lambda v: (-v[0][1], v[0][0])):
# lines = outputs[p, n]
print(f"---- {p} [{n}] ----")
2023-10-30 09:09:03 +00:00
for line in lines:
print(line)
2023-10-30 09:35:04 +00:00
print()
print("Done.")