Skip to content

Commit 5b03bd5

Browse files
committed
fix: infinite loop
enh: result table display enh: optimize next expression position find
1 parent 74bf26f commit 5b03bd5

File tree

3 files changed

+39
-40
lines changed

3 files changed

+39
-40
lines changed

crossmath.py

+28-18
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import random
2+
import time
23
from typing import Tuple, List
34

45
from table import Table
@@ -40,27 +41,35 @@ def _gen_random_expression_free(self, operator=None) -> List:
4041
return [operands[0], operator, operands[1], '=', result]
4142

4243
def _get_random_cell_for_next(self) -> Tuple[int, int, int, int]:
44+
start_time = time.time()
45+
counter = 0
46+
cells = self.table.find_not_empty_cells()
47+
if len(cells) == 0:
48+
cells = [self.table.get_cell(0, 0)]
4349
while True:
44-
cells = self.table.find_not_empty_cells()
45-
if not cells:
46-
cell_operand = self.table.get_cell(0, 0)
47-
else:
48-
cell_operand = random.choice(cells)
49-
horizontal = random.choice([True, False])
50-
if horizontal:
51-
if self.table.get_cell(cell_operand.get_x() + 1, cell_operand.get_y()).is_not_empty():
52-
continue
53-
if self.table.get_cell(cell_operand.get_x() - 1, cell_operand.get_y()).is_not_empty():
54-
continue
55-
else:
56-
if self.table.get_cell(cell_operand.get_x(), cell_operand.get_y() + 1).is_not_empty():
57-
continue
58-
if self.table.get_cell(cell_operand.get_x(), cell_operand.get_y() - 1).is_not_empty():
59-
continue
60-
return cell_operand.get_x(), cell_operand.get_y(), 1 if horizontal else 0, 0 if horizontal else 1
50+
counter += 1
51+
if time.time() - start_time > 0.005:
52+
raise Exception('timeout: ', time.time() - start_time, 'counter: ', counter)
53+
cell_operand = random.choice(cells)
54+
directions = []
55+
if self.table.get_cell(cell_operand.get_x() + 1, cell_operand.get_y()).is_not_empty() is False:
56+
directions.append((1, 0))
57+
if self.table.get_cell(cell_operand.get_x() - 1, cell_operand.get_y()).is_not_empty() is False:
58+
directions.append((-1, 0))
59+
if self.table.get_cell(cell_operand.get_x(), cell_operand.get_y() + 1).is_not_empty() is False:
60+
directions.append((0, 1))
61+
if self.table.get_cell(cell_operand.get_x(), cell_operand.get_y() - 1).is_not_empty() is False:
62+
directions.append((0, -1))
63+
64+
if len(directions) == 0:
65+
cells.remove(cell_operand)
66+
continue
67+
direction = random.choice(directions)
68+
return cell_operand.get_x(), cell_operand.get_y(), direction[0], direction[1]
6169

6270
def generate(self):
63-
for _ in range(2):
71+
start_time = time.time()
72+
for _ in range(20):
6473
x, y, xadd, yadd = self._get_random_cell_for_next()
6574
expression = self._gen_random_expression()
6675

@@ -69,6 +78,7 @@ def generate(self):
6978
cell.set_value(expression_part)
7079
x += xadd
7180
y += yadd
81+
print('time: ', time.time() - start_time)
7282

7383
def print(self):
7484
self.table.print()

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pandas

table.py

+10-22
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from typing import List
2+
import pandas
23

34

45
class Cell:
@@ -37,21 +38,8 @@ def get_x(self) -> int:
3738
def get_y(self) -> int:
3839
return self._y
3940

40-
def __str__(self):
41-
if self._value is None:
42-
value = '_'
43-
else:
44-
value = str(self._value)
45-
return value
46-
4741
def __repr__(self):
48-
x = str(self._x).rjust(2, ' ')
49-
y = str(self._y).rjust(2, ' ')
50-
if self._value is None:
51-
value = ' '
52-
else:
53-
value = str(self._value).center(3, ' ')
54-
return f'({x} x {y}) | {value} |'
42+
return '' if self._value is None else str(self._value)
5543

5644

5745
class Table:
@@ -63,11 +51,8 @@ def __init__(self):
6351
self._max_y: int = 0
6452

6553
def find_not_empty_cells(self) -> List[Cell]:
66-
result = []
67-
for cell in self._cells:
68-
if cell.is_not_empty():
69-
result.append(cell)
70-
return result
54+
# all cells is not empty in this case
55+
return self._cells[:]
7156

7257
def get_cell(self, x: int, y: int, empty_cell: bool = True) -> Cell | None:
7358
for cell in self._cells:
@@ -76,12 +61,15 @@ def get_cell(self, x: int, y: int, empty_cell: bool = True) -> Cell | None:
7661
return TableCell(self, x, y) if empty_cell else None
7762

7863
def print(self):
64+
rows = []
7965
for y in range(self.get_min_y(), self.get_max_y() + 1):
80-
row = []
66+
cols = []
8167
for x in range(self.get_min_x(), self.get_max_x() + 1):
8268
cell = self.get_cell(x, y)
83-
row.append(cell)
84-
print(row)
69+
cols.append(cell)
70+
rows.append(cols)
71+
df = pandas.DataFrame(rows)
72+
print(df)
8573

8674
def get_min_x(self) -> int:
8775
return self._min_x

0 commit comments

Comments
 (0)