Решение на Шахматни фенове от Стоян Михайлов

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

Към профила на Стоян Михайлов

Резултати

  • 6 точки от тестове
  • 0 бонус точки
  • 6 точки общо
  • 11 успешни тест(а)
  • 6 неуспешни тест(а)

Код

from collections import Counter
class ChessException(Exception):
pass
points_per_figure = {
"r": 5,
"n": 3,
"b": 3,
"q": 9,
"k": 4,
"p": 1,
}
class ChessScore:
def __init__(self, figures_list):
self.score = sum(map(lambda x: points_per_figure.get(x, 0), figures_list))
def __int__(self):
return self.score
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __eq__(self, other):
return self.score == other.score
def __ne__(self, other):
return self.score != other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self._check_for_one_king_per_color()
self._check_for_adjacent_kings()
self._check_for_pawn_position()
def __str__(self):
return self.fen
def __len__(self):
return len([f for f in self.fen if f.isalpha()])
def __getitem__(self, item):
col, row = item
rows = self.fen.split('/')
proper_rows = list(reversed(rows))
return proper_rows[ord(row) - ord('1')][ord(col) - ord('A')]
def _check_for_adjacent_kings(self):
rows = self.fen.split('/')
last_row_figures = ''
for index, row_figures in enumerate(rows):
if 'k' in row_figures and 'K' in row_figures:
white_king_position = row_figures.index('K')
black_king_position = row_figures.index('k')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
if index == 0:
last_row_figures = row_figures
continue
if ('k' in last_row_figures and 'K' in row_figures) or ('K' in last_row_figures and 'k' in row_figures):
try:
white_king_position = row_figures.index('K')
black_king_position = last_row_figures.index('k')
except ValueError:
white_king_position = row_figures.index('k')
black_king_position = last_row_figures.index('K')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
last_row_figures = row_figures
def _check_for_one_king_per_color(self):
figures = [x for x in self.fen]
counter = Counter(figures)
if counter['k'] != 1 or counter['K'] != 1:
raise ChessException('kings')
def _check_for_pawn_position(self):
rows = self.fen.split('/')
first_row_figures = [x for x in rows[0]]
last_row_figures = [x for x in rows[-1]]
if 'p' in first_row_figures or 'P' in first_row_figures or 'p' in last_row_figures or 'P' in last_row_figures:
raise ChessException('pawns')
def get_white_score(self):
white_figures = [f.lower() for f in self.fen if f.isupper()]
return ChessScore(white_figures)
def get_black_score(self):
black_figures = [f for f in self.fen if f.islower()]
return ChessScore(black_figures)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())

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

..EEEEE.....E....
======================================================================
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
solution.ChessException: kings

======================================================================
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
solution.ChessException: kings

======================================================================
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
solution.ChessException: kings

======================================================================
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
IndexError: string index out of range

======================================================================
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
solution.ChessException: kings

======================================================================
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
solution.ChessException: kings

----------------------------------------------------------------------
Ran 17 tests in 0.168s

FAILED (errors=6)

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

Стоян обнови решението на 28.11.2022 23:49 (преди над 1 година)

+from collections import Counter
+
+
+class ChessException(Exception):
+ def __init__(self, message):
+ self._message = message
+ super().__init__(self._message)
+
+
+points_per_figure = {
+ "r": 5,
+ "n": 3,
+ "b": 3,
+ "q": 9,
+ "k": 4,
+ "p": 1,
+}
+
+
+class ChessScore:
+ allowed_figures = ('r', 'n', 'b', 'k', 'q', 'p')
+
+ def __init__(self, figures_list):
+ self.score = sum(map(lambda x: points_per_figure[x], [f for f in figures_list if f in self.allowed_figures]))
+
+ def __int__(self):
+ return self.score
+
+ def __lt__(self, other):
+ return self.score < other.score
+
+ def __le__(self, other):
+ return self.score <= other.score
+
+ def __eq__(self, other):
+ return self.score == other.score
+
+ def __ne__(self, other):
+ return self.score != other.score
+
+ def __gt__(self, other):
+ return self.score > other.score
+
+ def __ge__(self, other):
+ return self.score >= other.score
+
+ def __add__(self, other):
+ return self.score + other.score
+
+ def __sub__(self, other):
+ return self.score - other.score
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+ self.__check_for_one_king_per_color__()
+ self.__check_for_adjacent_kings__()
+ self.__check_for_pawn_position__()
+
+ def __check_for_adjacent_kings__(self):
+ rows = self.fen.split('/')
+ last_row_figures = []
+ for index, row in enumerate(rows):
+ row_figures = [x for x in row]
+
+ if 'k' in row_figures and 'K' in row_figures:
+ white_king_position = row_figures.index('K')
+ black_king_position = row_figures.index('k')
+ if abs(white_king_position - black_king_position) <= 1:
+ raise ChessException('kings')
+
+ if index == 0:
+ last_row_figures = row_figures
+ continue
+
+ if ('k' in last_row_figures and 'K' in row_figures) or ('K' in last_row_figures and 'k' in row_figures):
+ try:
+ white_king_position = row_figures.index('K')
+ black_king_position = last_row_figures.index('k')
+ except ValueError:
+ white_king_position = row_figures.index('k')
+ black_king_position = last_row_figures.index('K')
+
+ if abs(white_king_position - black_king_position) <= 1:
+ raise ChessException('kings')
+
+ last_row_figures = row_figures
+
+ def __check_for_one_king_per_color__(self):
+ figures = [x for x in self.fen]
+ counter = Counter(figures)
+ if counter['k'] != 1 or counter['K'] != 1:
+ raise ChessException('kings')
+
+ def __check_for_pawn_position__(self):
+ rows = self.fen.split('/')
+ first_row_figures = [x for x in rows[0]]
+ last_row_figures = [x for x in rows[-1]]
+
+ try:
+ first_row_figures.index('p')
+ first_row_figures.index('P')
+ last_row_figures.index('p')
+ last_row_figures.index('P')
+ except ValueError:
+ pass
+ else:
+ raise ChessException('pawns')
+
+ def get_white_score(self):
+ white_figures = [f.lower() for f in self.fen if f.isupper()]
+ return ChessScore(white_figures)
+
+ def get_black_score(self):
+ black_figures = [f for f in self.fen if f.islower()]
+ return ChessScore(black_figures)
+
+ def white_is_winning(self):
+ return int(self.get_white_score()) > int(self.get_black_score())
+
+ def black_is_winning(self):
+ return int(self.get_white_score()) < int(self.get_black_score())
+
+ def is_equal(self):
+ return int(self.get_white_score()) == int(self.get_black_score())

Стоян обнови решението на 29.11.2022 00:57 (преди над 1 година)

from collections import Counter
class ChessException(Exception):
def __init__(self, message):
self._message = message
super().__init__(self._message)
points_per_figure = {
"r": 5,
"n": 3,
"b": 3,
"q": 9,
"k": 4,
"p": 1,
}
class ChessScore:
allowed_figures = ('r', 'n', 'b', 'k', 'q', 'p')
def __init__(self, figures_list):
self.score = sum(map(lambda x: points_per_figure[x], [f for f in figures_list if f in self.allowed_figures]))
def __int__(self):
return self.score
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __eq__(self, other):
return self.score == other.score
def __ne__(self, other):
return self.score != other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
class ChessPosition:
def __init__(self, fen):
self.fen = fen
self.__check_for_one_king_per_color__()
self.__check_for_adjacent_kings__()
self.__check_for_pawn_position__()
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ return len([f for f in self.fen if f.islower() or f.isupper()])
+
+ def __getitem__(self, item):
+ col, row = item
+ rows = self.fen.split('/')
+ proper_rows = list(reversed(rows))
+ return proper_rows[ord(row) - 49][ord(col) - 65]

Моля опитай се да не дефинираш магически числа като тези. Или ги вземи динамично, или ги дефинирай в константи, които подсказват смисъла зад тях.

+
def __check_for_adjacent_kings__(self):
rows = self.fen.split('/')
last_row_figures = []
for index, row in enumerate(rows):
row_figures = [x for x in row]
if 'k' in row_figures and 'K' in row_figures:
white_king_position = row_figures.index('K')
black_king_position = row_figures.index('k')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
if index == 0:
last_row_figures = row_figures
continue
if ('k' in last_row_figures and 'K' in row_figures) or ('K' in last_row_figures and 'k' in row_figures):
try:
white_king_position = row_figures.index('K')
black_king_position = last_row_figures.index('k')
except ValueError:
white_king_position = row_figures.index('k')
black_king_position = last_row_figures.index('K')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
last_row_figures = row_figures
def __check_for_one_king_per_color__(self):
figures = [x for x in self.fen]
counter = Counter(figures)
if counter['k'] != 1 or counter['K'] != 1:
raise ChessException('kings')
def __check_for_pawn_position__(self):
rows = self.fen.split('/')
first_row_figures = [x for x in rows[0]]
last_row_figures = [x for x in rows[-1]]
try:
first_row_figures.index('p')
first_row_figures.index('P')
last_row_figures.index('p')
last_row_figures.index('P')
except ValueError:
pass
else:
raise ChessException('pawns')
def get_white_score(self):
white_figures = [f.lower() for f in self.fen if f.isupper()]
return ChessScore(white_figures)
def get_black_score(self):
black_figures = [f for f in self.fen if f.islower()]
return ChessScore(black_figures)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())

Стоян обнови решението на 29.11.2022 13:44 (преди над 1 година)

from collections import Counter
class ChessException(Exception):
- def __init__(self, message):
- self._message = message
- super().__init__(self._message)
+ pass
points_per_figure = {
"r": 5,
"n": 3,
"b": 3,
"q": 9,
"k": 4,
"p": 1,
}
class ChessScore:
- allowed_figures = ('r', 'n', 'b', 'k', 'q', 'p')
-
def __init__(self, figures_list):
- self.score = sum(map(lambda x: points_per_figure[x], [f for f in figures_list if f in self.allowed_figures]))
+ self.score = sum(map(lambda x: points_per_figure.get(x, 0), figures_list))
def __int__(self):
return self.score
def __lt__(self, other):
return self.score < other.score
def __le__(self, other):
return self.score <= other.score
def __eq__(self, other):
return self.score == other.score
def __ne__(self, other):
return self.score != other.score
def __gt__(self, other):
return self.score > other.score
def __ge__(self, other):
return self.score >= other.score
def __add__(self, other):
return self.score + other.score
def __sub__(self, other):
return self.score - other.score
class ChessPosition:
def __init__(self, fen):
self.fen = fen
- self.__check_for_one_king_per_color__()
- self.__check_for_adjacent_kings__()
- self.__check_for_pawn_position__()
+ self._check_for_one_king_per_color()
+ self._check_for_adjacent_kings()
+ self._check_for_pawn_position()
def __str__(self):
return self.fen
def __len__(self):
- return len([f for f in self.fen if f.islower() or f.isupper()])
+ return len([f for f in self.fen if f.isalpha()])
def __getitem__(self, item):
col, row = item
rows = self.fen.split('/')
proper_rows = list(reversed(rows))
- return proper_rows[ord(row) - 49][ord(col) - 65]
+ return proper_rows[ord(row) - ord('1')][ord(col) - ord('A')]
- def __check_for_adjacent_kings__(self):
+ def _check_for_adjacent_kings(self):
rows = self.fen.split('/')
- last_row_figures = []
- for index, row in enumerate(rows):
- row_figures = [x for x in row]
-
+ last_row_figures = ''
+ for index, row_figures in enumerate(rows):
if 'k' in row_figures and 'K' in row_figures:
white_king_position = row_figures.index('K')
black_king_position = row_figures.index('k')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
if index == 0:
last_row_figures = row_figures
continue
if ('k' in last_row_figures and 'K' in row_figures) or ('K' in last_row_figures and 'k' in row_figures):
try:
white_king_position = row_figures.index('K')
black_king_position = last_row_figures.index('k')
except ValueError:
white_king_position = row_figures.index('k')
black_king_position = last_row_figures.index('K')
if abs(white_king_position - black_king_position) <= 1:
raise ChessException('kings')
last_row_figures = row_figures
- def __check_for_one_king_per_color__(self):
+ def _check_for_one_king_per_color(self):
figures = [x for x in self.fen]
counter = Counter(figures)
if counter['k'] != 1 or counter['K'] != 1:
raise ChessException('kings')
- def __check_for_pawn_position__(self):
+ def _check_for_pawn_position(self):
rows = self.fen.split('/')
first_row_figures = [x for x in rows[0]]
last_row_figures = [x for x in rows[-1]]
- try:
- first_row_figures.index('p')
- first_row_figures.index('P')
- last_row_figures.index('p')
- last_row_figures.index('P')
- except ValueError:
- pass
- else:
+ if 'p' in first_row_figures or 'P' in first_row_figures or 'p' in last_row_figures or 'P' in last_row_figures:
raise ChessException('pawns')
def get_white_score(self):
white_figures = [f.lower() for f in self.fen if f.isupper()]
return ChessScore(white_figures)
def get_black_score(self):
black_figures = [f for f in self.fen if f.islower()]
return ChessScore(black_figures)
def white_is_winning(self):
return int(self.get_white_score()) > int(self.get_black_score())
def black_is_winning(self):
return int(self.get_white_score()) < int(self.get_black_score())
def is_equal(self):
return int(self.get_white_score()) == int(self.get_black_score())