-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_map.py
More file actions
170 lines (135 loc) · 5.8 KB
/
expression_map.py
File metadata and controls
170 lines (135 loc) · 5.8 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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
import random
from enum import Enum
from typing import Tuple
import pandas
from expression import Expression
from operator_factory import Operator
from number_factory import NumberFactory
class Direction(Enum):
HORIZONTAL = 1
VERTICAL = 2
@staticmethod
def all() -> list:
return [Direction.HORIZONTAL, Direction.VERTICAL]
@staticmethod
def all_random() -> list:
directions = Direction.all()
random.shuffle(directions)
return directions
def is_horizontal(self) -> bool:
return self == Direction.HORIZONTAL
def is_vertical(self) -> bool:
return self == Direction.VERTICAL
def __str__(self):
return self.name
class ExpressionItem:
def __init__(self, x: int, y: int, direction: Direction, expression: Expression):
self._x = x
self._y = y
self._direction = direction
self._expression = expression
def x(self) -> int:
return self._x
def y(self) -> int:
return self._y
def is_horizontal(self) -> bool:
return self._direction == Direction.HORIZONTAL
def is_vertical(self) -> bool:
return self._direction == Direction.VERTICAL
def direction(self) -> Direction:
return self._direction
def expression(self) -> Expression:
return self._expression
def length(self) -> int:
return self._expression.get_length()
def width(self) -> int:
return self.length() if self.direction() == Direction.HORIZONTAL else 1
def height(self) -> int:
return 1 if self.direction() == Direction.HORIZONTAL else self.length()
def __str__(self):
return f"x: {self.x()}, y: {self.y()}, direction: {self.direction()}, length: {self.length()}, expression: {self.expression()}"
class ExpressionMapException(Exception):
pass
class ExpressionMapCellValueMissmatch(ExpressionMapException):
pass
class ExpressionMap:
def __init__(self, width: int, height: int):
self._width = width
self._height = height
self._map: list[list[Operator | None | float]] = [
[None for _ in range(width)] for _ in range(height)
]
self._operands_point: list[Tuple[int, int]] = []
self._version: int = 0
def get_version(self) -> int:
return self._version
def get_all_operand_points(self) -> list[Tuple[int, int]]:
return self._operands_point
def width(self) -> int:
return self._width
def height(self) -> int:
return self._height
def get(self, x: int, y: int) -> Operator | None | float:
if x < 0 or y < 0 or x >= self._width or y >= self._height:
raise ValueError(f"Invalid x, y: {x}, {y}")
return self._map[y][x]
def get_values(self, x: int, y: int, direction: Direction, length: int):
if x < 0 or y < 0 or x >= self._width or y >= self._height:
raise ValueError(f"Invalid x, y: {x}, {y}")
if direction == Direction.HORIZONTAL:
if x + length > self._width:
raise ValueError(f"Invalid x, length: {x}, {length}")
return self._map[y][x : x + length]
if y + length > self._height:
raise ValueError(f"Invalid y, length: {y}, {length}")
return [self._map[y + i][x] for i in range(length)]
def put(self, item: ExpressionItem):
x = item.x()
y = item.y()
if x < 0 or y < 0 or x >= self._width or y >= self._height:
raise ValueError(f"Invalid x, y: {x}, {y}")
values = item.expression().values()
if item.is_horizontal():
if x + len(values) > self._width:
raise ValueError(f"Invalid x, length: {x}, {len(values)}")
elif item.is_vertical():
if y + len(values) > self._height:
raise ValueError(f"Invalid y, length: {y}, {len(values)}")
else:
raise ValueError(f"Not supported direction: {item.direction()}")
# pre checking values and destination
for i in range(len(values)):
if item.is_horizontal():
if self._map[y][x + i] is not None and self._map[y][x + i] != values[i]:
raise ExpressionMapCellValueMissmatch(
f"Illegal override x, y, values: {x}, {y}, {self._map[y][x + i]}, {values[i]}"
)
else:
if self._map[y + i][x] is not None and self._map[y + i][x] != values[i]:
raise ExpressionMapCellValueMissmatch(
f"Illegal override x, y, values: {x}, {y}, {self._map[y + i][x]}, {values[i]}"
)
for i in range(len(values)):
target_x = x + i if item.is_horizontal() else x
target_y = y + i if item.is_vertical() else y
if isinstance(values[i], float):
self._operands_point.append((target_x, target_y))
self._map[target_y][target_x] = values[i]
self._version += 1
def print(self, number_factory: NumberFactory | None = None):
pandas.set_option("display.max_rows", None)
pandas.set_option("display.max_columns", None)
pandas.set_option("display.width", 2000)
map_clear = [["" for _ in range(self._width)] for _ in range(self._height)]
for y in range(self._height):
for x in range(self._width):
if self._map[y][x] is not None:
map_clear[y][x] = self._map[y][x]
is_numeric = isinstance(self._map[y][x], float)
if number_factory is not None and is_numeric:
map_clear[y][x] = number_factory.format(self._map[y][x])
df = pandas.DataFrame(map_clear)
print(df)
pandas.reset_option("display.max_rows")
pandas.reset_option("display.max_columns")
pandas.reset_option("display.width")