t | from itertools import permutations | t | from itertools import permutations |
| | | |
| def get_input_sequence() -> list[list[int]]: | | def get_input_sequence() -> list[list[int]]: |
| triples = [] | | triples = [] |
| while True: | | while True: |
| triple = input() | | triple = input() |
| if not triple: | | if not triple: |
| break | | break |
| triples.append([int(x) for x in triple.split(',')]) | | triples.append([int(x) for x in triple.split(',')]) |
| return triples | | return triples |
| | | |
| def is_not_less(a: list[int], b: list[int]) -> bool: | | def is_not_less(a: list[int], b: list[int]) -> bool: |
| for i, j, k in permutations([0, 1, 2]): | | for i, j, k in permutations([0, 1, 2]): |
| if a[0] <= b[i] and a[1] <= b[j] and (a[2] <= b[k]): | | if a[0] <= b[i] and a[1] <= b[j] and (a[2] <= b[k]): |
| if a[0] < b[i] or a[1] < b[j] or a[2] < b[k]: | | if a[0] < b[i] or a[1] < b[j] or a[2] < b[k]: |
| return False | | return False |
| return True | | return True |
| | | |
| def find_first_not_less(triples) -> int: | | def find_first_not_less(triples) -> int: |
| n = len(triples) | | n = len(triples) |
| for i in range(n): | | for i in range(n): |
| is_not_less_than_all = True | | is_not_less_than_all = True |
| for j in range(i + 1, n): | | for j in range(i + 1, n): |
| if not is_not_less(triples[i], triples[j]): | | if not is_not_less(triples[i], triples[j]): |
| is_not_less_than_all = False | | is_not_less_than_all = False |
| break | | break |
| if is_not_less_than_all: | | if is_not_less_than_all: |
| return i | | return i |
| return 0 | | return 0 |
| | | |
| def sort_triples(triples: list[list[int]]) -> list[list[int]]: | | def sort_triples(triples: list[list[int]]) -> list[list[int]]: |
| sorted_triples = [] | | sorted_triples = [] |
| while triples: | | while triples: |
| idx = find_first_not_less(triples) | | idx = find_first_not_less(triples) |
| sorted_triples.insert(0, triples.pop(idx)) | | sorted_triples.insert(0, triples.pop(idx)) |
| return reversed(sorted_triples) | | return reversed(sorted_triples) |
| triples = get_input_sequence() | | triples = get_input_sequence() |
| sorted_triples = sort_triples(triples) | | sorted_triples = sort_triples(triples) |
| for triple in sorted_triples: | | for triple in sorted_triples: |
| print(', '.join(map(str, triple))) | | print(', '.join(map(str, triple))) |