-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshow.py
More file actions
136 lines (108 loc) · 3.92 KB
/
Copy pathshow.py
File metadata and controls
136 lines (108 loc) · 3.92 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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
from functools import reduce
from enum import Enum
import numpy as np
class Color(Enum):
"""
ANSI escape codes for terminal colors.
Attributes:
RED: Red color code.
GREEN: Green color code.
YELLOW: Yellow color code.
PURPLE: Purple color code.
CYAN: Cyan color code.
END: Reset color code.
"""
RED = "\033[31m{}"
GREEN = "\033[32m{}"
YELLOW = "\033[33m{}"
PURPLE = "\033[35m{}"
CYAN = "\033[36m{}"
END = "\033[0m{}"
def colorize(text, color_format):
"""
Apply bold and color formatting to a string.
Args:
text (str): The string to be colorized.
color_format (str): ANSI format string from the Color enum.
Returns:
str: Colorized and bold-formatted string.
"""
return f"\033[1m{color_format.format(text)}{Color.END.value.format('')}"
def connect_two(table, point1, point2, width):
"""
Draw a line between two points in the table using colored symbols.
Args:
table (list[list[str]]): The visual table grid to modify.
point1 (tuple[int, int]): Starting point (row, column).
point2 (tuple[int, int]): Ending point (row, column).
width (int): Width of each cell (for alignment).
"""
symbol = (
colorize("=" * width, Color.RED.value)
if point1[0] == point2[0]
else colorize("|" + " " * (width - 1), Color.RED.value)
)
row_range = range(min(point1[0], point2[0]), max(point1[0], point2[0]) + 1)
col_range = range(min(point1[1], point2[1]), max(point1[1], point2[1]) + 1)
for j in row_range:
for i in col_range:
if (j, i) not in [point1, point2]:
table[j][i] = symbol
def format_element(el, width):
"""
Format a table element with proper padding and color.
Args:
el (int | str | None): The element to format.
width (int): Width to align to.
Returns:
str: Formatted string representation.
"""
if el is None:
return "x" * width
elif isinstance(el, int):
return colorize(str(el).ljust(width), Color.PURPLE.value)
return el
def get_max_width(rows_basis_cells, table):
"""
Determine the maximum string width among selected table cells.
Args:
rows_basis_cells (list[list[tuple[int, int]]]): Groups of cell coordinates.
table (list[list[Any]]): Original table.
Returns:
int: Maximum string length of numeric cell values.
"""
selected_elements = [
table[row][col]
for cell_group in rows_basis_cells
for row, col in cell_group
if table[row][col] is not None
]
max_element = max(selected_elements, default=0)
return len(str(max_element))
def show_cycle(table, rows_basis_cells, cycle):
"""
Display the table with a visual representation of a cycle path.
The path will show:
- 'S' in yellow for the start.
- '+' in green and cyan alternately for each step.
- '=' or '|' in red to connect steps.
Args:
table (list[list[Any]]): 2D table structure containing integers or None.
rows_basis_cells (list[list[tuple[int, int]]]): Cell coordinate groups used to determine max width.
cycle (list[tuple[int, int]]): List of (row, column) coordinates forming a cycle path.
"""
width = get_max_width(rows_basis_cells, table)
table_copy = [[format_element(el, width) for el in row] for row in table]
for i, (row, col) in enumerate(cycle):
if i == 0:
symbol = colorize("S".ljust(width), Color.YELLOW.value)
elif i % 2 == 0:
symbol = colorize("+".ljust(width), Color.GREEN.value)
else:
symbol = colorize("+".ljust(width), Color.CYAN.value)
table_copy[row][col] = symbol
next_point = cycle[(i + 1) % len(cycle)]
connect_two(table_copy, (row, col), next_point, width)
for row in table_copy:
print(" ".join(row))
print()