Решение на Шахматни фенове от Гергана Гочева

Обратно към всички решения

Към профила на Гергана Гочева

Резултати

  • 3 точки от тестове
  • 1 бонус точка
  • 4 точки общо
  • 5 успешни тест(а)
  • 12 неуспешни тест(а)

Код

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
class ChessException(Exception):
pass
class ChessPosition:
def find_figure(list_fen, figure):
figure_row = 0
for row in list_fen:
index = 0
figure_index = 1
while index < len(row) and row[index] != figure:
if row[index].isdigit():
figure_index += int(row[index])
else:
figure_index += 1
index += 1
if index < len(row) and row[index] == figure:
return 8 - figure_row, figure_index
figure_row += 1
def __init__(self, fen):
self.fen = fen
list_fen = fen.split("/")
black_king = find_figure(list_fen, 'k')
white_king = find_figure(list_fen, 'K')
if fen.count('k') != 1 or fen.count('K') != 1:
raise ChessException('kings')
if abs(black_king[0] - white_king[0]) <= 1 and abs(black_king[1] - white_king[1]) <= 1:
raise ChessException('kings')
if 'p' in list_fen[0] or 'p' in list_fen[7]:
raise ChessException('pawns')
if 'P' in list_fen[0] or 'P' in list_fen[7]:
raise ChessException('pawns')
def get_white_score(self):
return ChessScore([figure.lower() for figure in self.fen if figure.isupper()])
def get_black_score(self):
return ChessScore([figure for figure in self.fen if figure.islower()])
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
def __str__(self):
return self.fen
def __len__(self):
return len([figure for figure in self.fen if figure.isalpha()])
def __getitem__(self, item):
col = ord(item[0]) - 65
row = 8 - int(item[1])
list_fen = self.fen.split("/")
index = 0
for figure in list_fen[row]:
if index > col:
return None
if index == col and not figure.isdigit():
return figure
if figure.isdigit():
index += int(figure)
else:
index += 1
class ChessScore:
def __init__(self, figures):
self.figures = figures
def __int__(self):
return sum([scores[figure] for figure in self.figures])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __gt__(self, other):
return int(self) > int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Лог от изпълнението

.EEEEEEEEEEEE....
======================================================================
ERROR: test_against_touching_kings (test.TestChessPosition)
Test for kings next to each other.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_black_is_winning (test.TestChessPosition)
Test black_is_winning.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_get_black_score (test.TestChessPosition)
Test get_black_score.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_get_white_score (test.TestChessPosition)
Test get_white_score.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_getitem (test.TestChessPosition)
Test getitem functionality.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_is_equal (test.TestChessPosition)
Test is_equal.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_king_count (test.TestChessPosition)
Test for missing or multiple kings.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_len (test.TestChessPosition)
Test number of pieces for a position.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_pawns_position (test.TestChessPosition)
Test for incorrect pawns.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_str (test.TestChessPosition)
Test string representation of the instance.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_validation_conflict (test.TestChessPosition)
Test for correct Exception on multiple validation fails.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

======================================================================
ERROR: test_white_is_winning (test.TestChessPosition)
Test white_is_winning.
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/storage/deedee/data/rails/pyfmi-2022/releases/20221115154139/lib/language/python/runner.py", line 67, in thread
    raise result
NameError: name 'find_figure' is not defined

----------------------------------------------------------------------
Ran 17 tests in 0.171s

FAILED (errors=12)

История (3 версии и 7 коментара)

Гергана обнови решението на 28.11.2022 22:28 (преди почти 2 години)

+scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
+
+
+class ChessException(Exception):
+ def __init__(self, func):
+ self.message = func
+
+
+def find_figure(list_fen, figure):
+ figure_row = 0
+ for row in list_fen:
+ index = 0
+ figure_index = 1
+ while index < len(row) and row[index] != figure:
+ if row[index].isdigit():
+ figure_index += int(row[index])
+ else:
+ figure_index += 1
+ index += 1
+ if index < len(row) and row[index] == figure:
+ return 8 - figure_row, figure_index
+ figure_row += 1
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ list_fen = fen.split("/")
+ black_king = find_figure(list_fen, 'k')
+ white_king = find_figure(list_fen, 'K')
+ if abs(black_king[0] - white_king[0]) <= 1 and abs(black_king[1] - white_king[1]) <= 1:
+ raise ChessException('kings')
+ if fen.count('k') != 1 or fen.count('K') != 1:
+ raise ChessException('kings')
+ if 'p' in list_fen[0] or 'p' in list_fen[7]:
+ raise ChessException('pawns')
+ if 'P' in list_fen[0] or 'P' in list_fen[7]:
+ raise ChessException('pawns')
+
+ def get_white_score(self):
+ return ChessScore([figure.lower() for figure in self.fen if figure.isupper()])
+
+ def get_black_score(self):
+ return ChessScore([figure for figure in self.fen if figure.islower()])
+
+ def white_is_winning(self):
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ return self.get_white_score() < self.get_black_score()
+
+ def is_equal(self):
+ return self.get_white_score() == self.get_black_score()
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ return len([figure for figure in self.fen if figure.isLetter()])
+
+ def __getitem__(self, item):
+ col = ord(item[0]) - 65
+ row = 8 - item[1]
+ list_fen = self.fen.split("/")
+ index = 0
+ for figure in list_fen[row]:
+ if index > col:
+ return None
+ if index == col and not figure.isdigit():
+ return figure
+ if figure.isdigit():
+ index += int(figure)
+ else:
+ index += 1
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = figures
+
+ def __int__(self):
+ return sum([scores[_] for _ in self.figures])
+
+ def __lt__(self, other):
+ return int(self) < int(other)
+
+ def __le__(self, other):
+ return int(self) <= int(other)
+
+ def __eq__(self, other):
+ return int(self) == int(other)
+
+ def __ne__(self, other):
+ return int(self) != int(other)
+
+ def __gt__(self, other):
+ return int(self) > int(other)
+
+ def __add__(self, other):
+ return int(self) + int(other)
+
+ def __sub__(self, other):
+ return int(self) - int(other)

Гергана обнови решението на 29.11.2022 07:58 (преди почти 2 години)

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
class ChessException(Exception):
def __init__(self, func):

func не е удачно име за това, защото реално не приемаш функция, а текст.
Като цяло можеш да пропуснеш инициализатора. Един pass в тялото на класа е достатъчен.

- self.message = func
+ self._message = func
def find_figure(list_fen, figure):
figure_row = 0
for row in list_fen:
index = 0
figure_index = 1
while index < len(row) and row[index] != figure:
if row[index].isdigit():
figure_index += int(row[index])
else:
figure_index += 1
index += 1
if index < len(row) and row[index] == figure:
return 8 - figure_row, figure_index
figure_row += 1
class ChessPosition:
def __init__(self, fen):
self.fen = fen
list_fen = fen.split("/")
black_king = find_figure(list_fen, 'k')
white_king = find_figure(list_fen, 'K')
- if abs(black_king[0] - white_king[0]) <= 1 and abs(black_king[1] - white_king[1]) <= 1:
- raise ChessException('kings')
if fen.count('k') != 1 or fen.count('K') != 1:
raise ChessException('kings')
+ if abs(black_king[0] - white_king[0]) <= 1 and abs(black_king[1] - white_king[1]) <= 1:
+ raise ChessException('kings')
if 'p' in list_fen[0] or 'p' in list_fen[7]:
raise ChessException('pawns')
if 'P' in list_fen[0] or 'P' in list_fen[7]:
raise ChessException('pawns')
def get_white_score(self):
return ChessScore([figure.lower() for figure in self.fen if figure.isupper()])
def get_black_score(self):
return ChessScore([figure for figure in self.fen if figure.islower()])
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
def __str__(self):
return self.fen
def __len__(self):
- return len([figure for figure in self.fen if figure.isLetter()])
+ return len([figure for figure in self.fen if figure.isalpha()])
def __getitem__(self, item):
col = ord(item[0]) - 65
- row = 8 - item[1]
+ row = 8 - int(item[1])
list_fen = self.fen.split("/")
index = 0
for figure in list_fen[row]:
if index > col:
return None
if index == col and not figure.isdigit():
return figure
if figure.isdigit():
index += int(figure)
else:
index += 1
class ChessScore:
def __init__(self, figures):
self.figures = figures
def __int__(self):
return sum([scores[_] for _ in self.figures])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __gt__(self, other):
return int(self) > int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)

Гергана обнови решението на 29.11.2022 17:25 (преди почти 2 години)

scores = {'p': 1, 'n': 3, 'b': 3, 'k': 4, 'r': 5, 'q': 9}
class ChessException(Exception):
- def __init__(self, func):
- self._message = func
+ pass
-def find_figure(list_fen, figure):
- figure_row = 0
- for row in list_fen:
- index = 0
- figure_index = 1
- while index < len(row) and row[index] != figure:
- if row[index].isdigit():
- figure_index += int(row[index])
- else:
- figure_index += 1
- index += 1
- if index < len(row) and row[index] == figure:
- return 8 - figure_row, figure_index
- figure_row += 1
-
-
class ChessPosition:
+ def find_figure(list_fen, figure):
+ figure_row = 0
+ for row in list_fen:
+ index = 0
+ figure_index = 1
+ while index < len(row) and row[index] != figure:
+ if row[index].isdigit():
+ figure_index += int(row[index])
+ else:
+ figure_index += 1
+ index += 1
+ if index < len(row) and row[index] == figure:
+ return 8 - figure_row, figure_index
+ figure_row += 1
+
def __init__(self, fen):
self.fen = fen
list_fen = fen.split("/")
black_king = find_figure(list_fen, 'k')
white_king = find_figure(list_fen, 'K')
if fen.count('k') != 1 or fen.count('K') != 1:
raise ChessException('kings')
if abs(black_king[0] - white_king[0]) <= 1 and abs(black_king[1] - white_king[1]) <= 1:
raise ChessException('kings')
if 'p' in list_fen[0] or 'p' in list_fen[7]:
raise ChessException('pawns')
if 'P' in list_fen[0] or 'P' in list_fen[7]:
raise ChessException('pawns')
def get_white_score(self):
return ChessScore([figure.lower() for figure in self.fen if figure.isupper()])
def get_black_score(self):
return ChessScore([figure for figure in self.fen if figure.islower()])
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_white_score() < self.get_black_score()
def is_equal(self):
return self.get_white_score() == self.get_black_score()
def __str__(self):
return self.fen
def __len__(self):
return len([figure for figure in self.fen if figure.isalpha()])
def __getitem__(self, item):
col = ord(item[0]) - 65
row = 8 - int(item[1])
list_fen = self.fen.split("/")
index = 0
for figure in list_fen[row]:
if index > col:
return None
if index == col and not figure.isdigit():
return figure
if figure.isdigit():
index += int(figure)
else:
index += 1
class ChessScore:
def __init__(self, figures):
self.figures = figures
def __int__(self):
- return sum([scores[_] for _ in self.figures])
+ return sum([scores[figure] for figure in self.figures])
def __lt__(self, other):
return int(self) < int(other)
def __le__(self, other):
return int(self) <= int(other)
def __eq__(self, other):
return int(self) == int(other)
def __ne__(self, other):
return int(self) != int(other)
def __gt__(self, other):
return int(self) > int(other)
def __add__(self, other):
return int(self) + int(other)
def __sub__(self, other):
return int(self) - int(other)