Решение на Шахматни фенове от Александра Радева

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

Към профила на Александра Радева

Резултати

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

Код

points_per_figure = [ ( "r" , 5),
( "n" , 3 ),
( "b" , 3 ),
( "q" , 9 ),
( "k" , 4 ),
( "p" , 1 )]
positions = "ABCDEFGH"
class ChessException(Exception):
def __init__(self, message = "Default message"):
self._message = message
super().__init__(self._message)
class ChessScore:
def __init__(self, figures):
self.figures = figures
points = 0
for figure in self.figures:
for chess_figure, chess_points in points_per_figure:
if figure.lower() == chess_figure:
points += chess_points
self.points = points
def __int__(self):
return self.points
def __lt__(self, other):
return self.points < other.points
def __gt__(self, other):
return self.points > other.points
def __le__(self, other):
return self.points <= other.points
def __ge__(self, other):
return self.points >= other.points
def __eq__(self, other):
return self.points == other.points
def __ne__(self, other):
return self.points != other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return abs(self.points - other.points)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
if self.fen.lower().count("kk") > 0:
raise ChessException("kings")
splitted = self.fen.split('/')
if self.fen.count("k") > 1 or self.fen.count("k") <= 0 or self.fen.count("K") > 1 or self.fen.count("K") <= 0:
raise ChessException("kings")
if splitted[0].count("p") > 0 or splitted[0].count("P") > 0 or splitted[7].count("p") > 0 or splitted[7].count("P") > 0:
raise ChessException("pawns")
def fen_easy(self):
easy_fen = ""
for i in range(len(self.fen)):
if self.fen[i].isnumeric():
for j in range(int(self.fen[i])):
easy_fen += self.fen[i]
elif self.fen[i].isalpha():
easy_fen += self.fen[i]
return easy_fen
def __getitem__(self, subscript):
chess_position = abs(int(subscript[1]) - 8 ) * 8 + int(positions.find(subscript[0]))
print(self.fen_easy())
if not self.fen_easy()[chess_position].isalpha():
return None
else:
return self.fen_easy()[chess_position]
def __print__(self):
print(self.fen)
def __str__(self):
return self.fen
def __len__(self):
lenght = 64
for i in range(0, len(self.fen)):
if self.fen[i].isnumeric():
lenght -= int(self.fen[i])
return lenght
def get_white_score(self):
white_figures = []
for i in range(0, len(self.fen)):
if self.fen[i].isupper():
white_figures.append(self.fen[i])
return ChessScore(white_figures).__int__()
def get_black_score(self):
black_figures = []
for i in range(0, len(self.fen)):
if not self.fen[i].isupper():
black_figures.append(self.fen[i])
return ChessScore(black_figures).__int__()
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()

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

.E..F......FFF...
======================================================================
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
Exception: No exception raised on: k7/K7/8/8/8/8/8/8

======================================================================
FAIL: 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
AssertionError: 4 is not an instance of <class 'solution.ChessScore'>

======================================================================
FAIL: 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
AssertionError: 'pawns' != 'kings'
- pawns
+ kings


======================================================================
FAIL: 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
AssertionError: 4 is not an instance of <class 'solution.ChessScore'>

======================================================================
FAIL: test_basic_arithmetic (test.TestChessScore)
Test additiona and subtraction of ChessScores.
----------------------------------------------------------------------
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
AssertionError: 1 != -1

----------------------------------------------------------------------
Ran 17 tests in 0.164s

FAILED (failures=4, errors=1)

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

Александра обнови решението на 29.11.2022 17:08 (преди почти 2 години)

+points_per_figure = [ ( "r" , 5),
+ ( "n" , 3 ),
+ ( "b" , 3 ),
+ ( "q" , 9 ),
+ ( "k" , 4 ),
+ ( "p" , 1 )]
+
+positions = "ABCDEFGH"
+
+class ChessException(Exception):
+ def __init__(self, message = "Default message"):
+ self._message = message
+ super().__init__(self._message)
+
+
+class ChessScore:
+ def __init__(self, figures):
+ self.figures = figures
+ points = 0
+ for figure in self.figures:
+ for chess_figure, chess_points in points_per_figure:
+ if figure.lower() == chess_figure:
+ points += chess_points
+ self.points = points
+
+ def __int__(self):
+ return self.points
+
+ def __lt__(self, other):
+ return self.points < other.points
+
+ def __gt__(self, other):
+ return self.points > other.points
+
+ def __le__(self, other):
+ return self.points <= other.points
+
+ def __ge__(self, other):
+ return self.points >= other.points
+
+ def __eq__(self, other):
+ return self.points == other.points
+
+ def __ne__(self, other):
+ return self.points != other.points
+
+ def __add__(self, other):
+ return self.points + other.points
+
+ def __sub__(self, other):
+ return abs(self.points - other.points)
+
+
+class ChessPosition:
+ def __init__(self, fen):
+ self.fen = fen
+
+
+ def fen_easy(self):
+ easy_fen = ""
+ for i in range(len(self.fen)):
+ if self.fen[i].isnumeric():
+ for j in range(int(self.fen[i])):
+ easy_fen += self.fen[i]
+ elif self.fen[i].isalpha():
+ easy_fen += self.fen[i]
+
+ return easy_fen
+
+ def __getitem__(self, subscript):
+ chess_position = abs(int(subscript[1]) - 8 ) * 8 + int(positions.find(subscript[0]))
+ print(self.fen_easy())
+ if not self.fen_easy()[chess_position].isalpha():
+ return None
+ else:
+ return self.fen_easy()[chess_position]
+
+ def __print__(self):
+ print(self.fen)
+
+ def __str__(self):
+ return self.fen
+
+ def __len__(self):
+ lenght = 64
+ for i in range(0, len(self.fen)):
+ if self.fen[i].isnumeric():
+ lenght -= int(self.fen[i])
+ return lenght
+
+ def get_white_score(self):
+ white_figures = []
+ for i in range(0, len(self.fen)):
+ if self.fen[i].isupper():
+ white_figures.append(self.fen[i])
+
+ return ChessScore(white_figures).__int__()
+
+ def get_black_score(self):
+ black_figures = []
+ for i in range(0, len(self.fen)):
+ if not self.fen[i].isupper():
+ black_figures.append(self.fen[i])
+
+ return ChessScore(black_figures).__int__()
+
+ 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()

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

points_per_figure = [ ( "r" , 5),
( "n" , 3 ),
( "b" , 3 ),
( "q" , 9 ),
( "k" , 4 ),
( "p" , 1 )]
positions = "ABCDEFGH"
class ChessException(Exception):
def __init__(self, message = "Default message"):
self._message = message
super().__init__(self._message)
class ChessScore:
def __init__(self, figures):
self.figures = figures
points = 0
for figure in self.figures:
for chess_figure, chess_points in points_per_figure:
if figure.lower() == chess_figure:
points += chess_points
self.points = points
def __int__(self):
return self.points
def __lt__(self, other):
return self.points < other.points
def __gt__(self, other):
return self.points > other.points
def __le__(self, other):
return self.points <= other.points
def __ge__(self, other):
return self.points >= other.points
def __eq__(self, other):
return self.points == other.points
def __ne__(self, other):
return self.points != other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return abs(self.points - other.points)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
+ if self.fen.count("k") > 1 or self.fen.count("k") <= 0 or self.fen.count("K") > 1 or self.fen.count("K") <= 0:
+ raise ChessException("kings")
def fen_easy(self):
easy_fen = ""
for i in range(len(self.fen)):
if self.fen[i].isnumeric():
for j in range(int(self.fen[i])):
easy_fen += self.fen[i]
elif self.fen[i].isalpha():
easy_fen += self.fen[i]
return easy_fen
def __getitem__(self, subscript):
chess_position = abs(int(subscript[1]) - 8 ) * 8 + int(positions.find(subscript[0]))
print(self.fen_easy())
if not self.fen_easy()[chess_position].isalpha():
return None
else:
return self.fen_easy()[chess_position]
def __print__(self):
print(self.fen)
def __str__(self):
return self.fen
def __len__(self):
lenght = 64
for i in range(0, len(self.fen)):
if self.fen[i].isnumeric():
lenght -= int(self.fen[i])
return lenght
def get_white_score(self):
white_figures = []
for i in range(0, len(self.fen)):
if self.fen[i].isupper():
white_figures.append(self.fen[i])
return ChessScore(white_figures).__int__()
def get_black_score(self):
black_figures = []
for i in range(0, len(self.fen)):
if not self.fen[i].isupper():
black_figures.append(self.fen[i])
return ChessScore(black_figures).__int__()
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()

Александра обнови решението на 29.11.2022 17:37 (преди почти 2 години)

points_per_figure = [ ( "r" , 5),
( "n" , 3 ),
( "b" , 3 ),
( "q" , 9 ),
( "k" , 4 ),
( "p" , 1 )]
positions = "ABCDEFGH"
class ChessException(Exception):
def __init__(self, message = "Default message"):
self._message = message
super().__init__(self._message)
class ChessScore:
def __init__(self, figures):
self.figures = figures
points = 0
for figure in self.figures:
for chess_figure, chess_points in points_per_figure:
if figure.lower() == chess_figure:
points += chess_points
self.points = points
def __int__(self):
return self.points
def __lt__(self, other):
return self.points < other.points
def __gt__(self, other):
return self.points > other.points
def __le__(self, other):
return self.points <= other.points
def __ge__(self, other):
return self.points >= other.points
def __eq__(self, other):
return self.points == other.points
def __ne__(self, other):
return self.points != other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return abs(self.points - other.points)
class ChessPosition:
def __init__(self, fen):
self.fen = fen
+
+ if self.fen.lower().count("kk") > 0:
+ raise ChessException("kings")
+
+ splitted = self.fen.split('/')
+
if self.fen.count("k") > 1 or self.fen.count("k") <= 0 or self.fen.count("K") > 1 or self.fen.count("K") <= 0:
raise ChessException("kings")
+ if splitted[0].count("p") > 0 or splitted[0].count("P") > 0 or splitted[7].count("p") > 0 or splitted[7].count("P") > 0:
+ raise ChessException("pawns")
+
def fen_easy(self):
easy_fen = ""
for i in range(len(self.fen)):
if self.fen[i].isnumeric():
for j in range(int(self.fen[i])):
easy_fen += self.fen[i]
elif self.fen[i].isalpha():
easy_fen += self.fen[i]
return easy_fen
def __getitem__(self, subscript):
chess_position = abs(int(subscript[1]) - 8 ) * 8 + int(positions.find(subscript[0]))
print(self.fen_easy())
if not self.fen_easy()[chess_position].isalpha():
return None
else:
return self.fen_easy()[chess_position]
def __print__(self):
print(self.fen)
def __str__(self):
return self.fen
def __len__(self):
lenght = 64
for i in range(0, len(self.fen)):
if self.fen[i].isnumeric():
lenght -= int(self.fen[i])
return lenght
def get_white_score(self):
white_figures = []
for i in range(0, len(self.fen)):
if self.fen[i].isupper():
white_figures.append(self.fen[i])
return ChessScore(white_figures).__int__()
def get_black_score(self):
black_figures = []
for i in range(0, len(self.fen)):
if not self.fen[i].isupper():
black_figures.append(self.fen[i])
return ChessScore(black_figures).__int__()
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()
+