Решение на Шахматни фенове от Дуйгу Хасан

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

Към профила на Дуйгу Хасан

Резултати

  • 9 точки от тестове
  • 0 бонус точки
  • 9 точки общо
  • 15 успешни тест(а)
  • 2 неуспешни тест(а)

Код

chess_piece = ('R', 'N', 'B', 'Q', 'K', 'P')
scores = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
class ChessException(Exception):
def __init__(self, message):
self._message = message
super().__init__(self._message)
def get_chess_board(string_board: str):
chess_board = [[]] # will use - for spare places
n = 0
for char in string_board:
if char != '/':
if char in chess_piece or char.upper() in chess_piece:
chess_board[n].append(char)
elif ord(char) in range(49, 57):
for k in range(int(char)):
chess_board[n].append('-')
else:
raise ChessException("Incorrect input")
else:
if len(chess_board[n]) != 8:
raise ChessException("Incorrect input")
chess_board.append([])
n += 1
if len(chess_board) != 8:
raise ChessException("Incorrect input")
if invalidate_kings(chess_board):
raise ChessException("kings")
if invalid_pawns(chess_board):
raise ChessException("pawns")
return chess_board
def invalidate_kings(chess_board):
king_positions = []
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j] == 'K' or chess_board[i][j] == 'k':
if king_positions and king_positions[0][2] == chess_board[i][j]:
raise ChessException("kings")
else:
king_positions.append([i, j, chess_board[i][j]])
if len(king_positions) != 2:
raise ChessException("kings")
# first king -[king_positions[0][0], king_positions[0][1]] --> i, j
# second king -[king_positions[1][0], king_positions[1][1]] --> i1, j1
# invalid --> (i = i1, j1 = j +-1) (i1 = i + 1, j1 = j +-1) (i1 = i - 1, j1 = j+-1) (j = j1, i1 = i +-1
close = ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
king_positions[1][0] == king_positions[0][0] - 1)
and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1))
closer = ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
king_positions[0][0] == king_positions[1][0] + 1))
return close or closer
def invalid_pawns(chess_board):
return 'p' in chess_board[0] + chess_board[7] or 'P' in chess_board[0] + chess_board[7]
class ChessPosition:
def __init__(self, fen: str):
self._fen = fen.split()
self._chess_board = get_chess_board(self._fen[0])
self._white_score = 0
self._black_score = 0
self._figures_count = 0
for row in self._chess_board:
for element in row:
if element in chess_piece:
self._white_score += scores[element]
self._figures_count += 1
elif ord(element) > 96 and element.upper() in chess_piece:
self._black_score += scores[element.upper()]
self._figures_count += 1
def get_white_score(self):
return self._white_score
def get_black_score(self):
return self._black_score
def white_is_winning(self):
return self._white_score > self._black_score
def black_is_winning(self):
return self._black_score > self._white_score
def is_equal(self):
return self._white_score == self._black_score
def __getitem__(self, key):
item = self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
if item == '-':
return None
return item
def __len__(self):
return self._figures_count
def __str__(self):
return self._fen[0]
class ChessScore:
def __init__(self, figures: list[str]):
self._score = 0
for figure in figures:
if figure.upper() in scores.keys():
self._score += scores[figure.upper()]
else:
raise ChessException("This figure does not exist")
def get_score(self):
return self._score
def __lt__(self, other):
return self._score < other.get_score()
def __le__(self, other):
return self._score <= other.get_score()
def __eq__(self, other):
return self._score == other.get_score()
def __ne__(self, other):
return self._score != other.get_score()
def __gt__(self, other):
return self._score > other.get_score()
def __ge__(self, other):
return self._score >= other.get_score()
def __add__(self, other):
return self._score + other.get_score()
def __sub__(self, other):
return self._score - other.get_score()
def __int__(self):
return self._score

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

....F.......F....
======================================================================
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_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'>

----------------------------------------------------------------------
Ran 17 tests in 0.177s

FAILED (failures=2)

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

Дуйгу обнови решението на 28.11.2022 23:56 (преди почти 2 години)

+
+chess_piece = ('R', 'N', 'B', 'Q', 'K', 'P')
+scores = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
+
+
+class ChessException(Exception):
+ pass
+
+
+def get_chess_board(string_board: str):
+ chess_board = [[]] # will use - for spare places
+ n = 0
+ for char in string_board:
+ if char != '/':
+ if char in chess_piece or char.upper() in chess_piece:
+ chess_board[n].append(char)
+ elif ord(char) in range(49, 57):
+ for k in range(int(char)):
+ chess_board[n].append('-')
+ else:
+ raise ChessException("Incorrect input")
+ else:
+ if len(chess_board[n]) != 8:
+ raise ChessException("Incorrect input")
+ chess_board.append([])
+ n += 1
+ if len(chess_board) != 8:
+ raise ChessException("Incorrect input")
+ if invalidate_kings(chess_board) and invalid_pawns(chess_board):
+ raise ChessException("kings")
+ return chess_board
+
+
+def invalidate_kings(chess_board):
+ king_positions = []
+ for i in range(len(chess_board)):
+ for j in range(len(chess_board[i])):
+ if chess_board[i][j] == 'K' or chess_board[i][j] == 'k':
+ if king_positions and king_positions[0][2] == chess_board[i][j]:
+ raise ChessException("kings")
+ else:
+ king_positions.append([i, j, chess_board[i][j]])
+ if len(king_positions) != 2:
+ raise ChessException("kings")
+ # first king -[king_positions[0][0], king_positions[0][1]] --> i, j
+ # second king -[king_positions[1][0], king_positions[1][1]] --> i1, j1
+ # invalid --> (i = i1, j1 = j +-1) (i1 = i + 1, j1 = j +-1) (i1 = i - 1, j1 = j+-1) (j = j1, i1 = i +-1
+ return ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
+ king_positions[1][0] == king_positions[0][0] - 1)
+ and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1)
+ and ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
+ king_positions[0][0] == king_positions[1][0] + 1)))
+
+
+def invalid_pawns(chess_board):
+ for i in chess_board[0] + chess_board[7]:
+ if i == 'p' or i == 'P':
+ return True
+ return False
+
+
+class ChessPosition:
+ def __init__(self, fen: str):
+ my_fen = fen.split()
+ self._chess_board = get_chess_board(my_fen[0])
+ self._white_score = 0
+ self._black_score = 0
+ for row in self._chess_board:
+ for element in row:
+ if element in chess_piece:
+ self._white_score += scores[element]
+ elif ord(element) > 96 and element.upper() in chess_piece:
+ self._black_score += scores[element.upper()]
+
+ def get_white_score(self):
+ return self._white_score
+
+ def get_black_score(self):
+ return self._black_score
+
+ def white_is_winning(self):
+ return self._white_score > self._black_score
+
+ def black_is_winning(self):
+ return self._black_score > self._white_score
+
+ def is_equal(self):
+ return self._white_score == self._black_score
+
+ def __getitem__(self, key):
+ if self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')] == '-':
+ return None
+ return self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
+
+
+class ChessScore:
+ def __init__(self, figures: list[str]):
+ self._score = 0
+ for figure in figures:
+ if figure.upper() in scores.keys():
+ self._score += scores[figure.upper()]
+ else:
+ raise ChessException("This figure does not exist")
+
+ def get_score(self):
+ return self._score
+
+ def __lt__(self, other):
+ return self._score < other.get_score()
+
+ def __le__(self, other):
+ return self._score <= other.get_score()
+
+ def __eq__(self, other):
+ return self._score == other.get_score()
+
+ def __ne__(self, other):
+ return self._score != other.get_score()
+
+ def __gt__(self, other):
+ return self._score > other.get_score()
+
+ def __ge__(self, other):
+ return self._score >= other.get_score()
+
+ def __add__(self, other):
+ return self._score + other.get_score()
+
+ def __sub__(self, other):
+ return self._score - other.get_score()
+
+

Дуйгу обнови решението на 29.11.2022 12:25 (преди почти 2 години)

-
chess_piece = ('R', 'N', 'B', 'Q', 'K', 'P')
scores = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
class ChessException(Exception):
pass
def get_chess_board(string_board: str):
chess_board = [[]] # will use - for spare places
n = 0
for char in string_board:
if char != '/':
if char in chess_piece or char.upper() in chess_piece:
chess_board[n].append(char)
elif ord(char) in range(49, 57):
for k in range(int(char)):
chess_board[n].append('-')
else:
raise ChessException("Incorrect input")
else:
if len(chess_board[n]) != 8:
raise ChessException("Incorrect input")
chess_board.append([])
n += 1
if len(chess_board) != 8:
raise ChessException("Incorrect input")
- if invalidate_kings(chess_board) and invalid_pawns(chess_board):
+ if invalidate_kings(chess_board):
raise ChessException("kings")
+ if invalid_pawns(chess_board):
+ raise ChessException("pawns")
return chess_board
def invalidate_kings(chess_board):
king_positions = []
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j] == 'K' or chess_board[i][j] == 'k':
if king_positions and king_positions[0][2] == chess_board[i][j]:
raise ChessException("kings")
else:
king_positions.append([i, j, chess_board[i][j]])
if len(king_positions) != 2:
raise ChessException("kings")
# first king -[king_positions[0][0], king_positions[0][1]] --> i, j
# second king -[king_positions[1][0], king_positions[1][1]] --> i1, j1
# invalid --> (i = i1, j1 = j +-1) (i1 = i + 1, j1 = j +-1) (i1 = i - 1, j1 = j+-1) (j = j1, i1 = i +-1
return ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
king_positions[1][0] == king_positions[0][0] - 1)
and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1)
and ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
king_positions[0][0] == king_positions[1][0] + 1)))
def invalid_pawns(chess_board):
- for i in chess_board[0] + chess_board[7]:
- if i == 'p' or i == 'P':
- return True
- return False
+ return 'p' in chess_board[0] + chess_board[7] or 'P' in chess_board[0] + chess_board[7]
class ChessPosition:
def __init__(self, fen: str):
my_fen = fen.split()
self._chess_board = get_chess_board(my_fen[0])
self._white_score = 0
self._black_score = 0
for row in self._chess_board:
for element in row:
if element in chess_piece:
self._white_score += scores[element]
elif ord(element) > 96 and element.upper() in chess_piece:
self._black_score += scores[element.upper()]
def get_white_score(self):
return self._white_score
def get_black_score(self):
return self._black_score
def white_is_winning(self):
return self._white_score > self._black_score
def black_is_winning(self):
return self._black_score > self._white_score
def is_equal(self):
return self._white_score == self._black_score
def __getitem__(self, key):
- if self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')] == '-':
+ item = self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
+ if item == '-':
return None
- return self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
+ return item
class ChessScore:
def __init__(self, figures: list[str]):
self._score = 0
for figure in figures:
if figure.upper() in scores.keys():
self._score += scores[figure.upper()]
else:
raise ChessException("This figure does not exist")
def get_score(self):
return self._score
def __lt__(self, other):
return self._score < other.get_score()
def __le__(self, other):
return self._score <= other.get_score()
def __eq__(self, other):
return self._score == other.get_score()
def __ne__(self, other):
return self._score != other.get_score()
def __gt__(self, other):
return self._score > other.get_score()
def __ge__(self, other):
return self._score >= other.get_score()
def __add__(self, other):
return self._score + other.get_score()
def __sub__(self, other):
return self._score - other.get_score()
-

Дуйгу обнови решението на 29.11.2022 12:48 (преди почти 2 години)

chess_piece = ('R', 'N', 'B', 'Q', 'K', 'P')
scores = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
class ChessException(Exception):
- pass
+ def __init__(self, message):
+ self._message = message
+ super().__init__(self._message)
def get_chess_board(string_board: str):
chess_board = [[]] # will use - for spare places
n = 0
for char in string_board:
if char != '/':
if char in chess_piece or char.upper() in chess_piece:
chess_board[n].append(char)
elif ord(char) in range(49, 57):
for k in range(int(char)):
chess_board[n].append('-')
else:
raise ChessException("Incorrect input")
else:
if len(chess_board[n]) != 8:
raise ChessException("Incorrect input")
chess_board.append([])
n += 1
if len(chess_board) != 8:
raise ChessException("Incorrect input")
if invalidate_kings(chess_board):
raise ChessException("kings")
if invalid_pawns(chess_board):
raise ChessException("pawns")
return chess_board
def invalidate_kings(chess_board):
king_positions = []
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j] == 'K' or chess_board[i][j] == 'k':
if king_positions and king_positions[0][2] == chess_board[i][j]:
raise ChessException("kings")
else:
king_positions.append([i, j, chess_board[i][j]])
if len(king_positions) != 2:
raise ChessException("kings")
# first king -[king_positions[0][0], king_positions[0][1]] --> i, j
# second king -[king_positions[1][0], king_positions[1][1]] --> i1, j1
# invalid --> (i = i1, j1 = j +-1) (i1 = i + 1, j1 = j +-1) (i1 = i - 1, j1 = j+-1) (j = j1, i1 = i +-1
- return ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
- king_positions[1][0] == king_positions[0][0] - 1)
- and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1)
- and ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
- king_positions[0][0] == king_positions[1][0] + 1)))
+ close = ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
+ king_positions[1][0] == king_positions[0][0] - 1)
+ and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1))
+ closer = ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
+ king_positions[0][0] == king_positions[1][0] + 1))
+ return close or closer
def invalid_pawns(chess_board):
return 'p' in chess_board[0] + chess_board[7] or 'P' in chess_board[0] + chess_board[7]
class ChessPosition:
def __init__(self, fen: str):
my_fen = fen.split()
self._chess_board = get_chess_board(my_fen[0])
self._white_score = 0
self._black_score = 0
for row in self._chess_board:
for element in row:
if element in chess_piece:
self._white_score += scores[element]
elif ord(element) > 96 and element.upper() in chess_piece:
self._black_score += scores[element.upper()]
def get_white_score(self):
return self._white_score
def get_black_score(self):
return self._black_score
def white_is_winning(self):
return self._white_score > self._black_score
def black_is_winning(self):
return self._black_score > self._white_score
def is_equal(self):
return self._white_score == self._black_score
def __getitem__(self, key):
item = self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
if item == '-':
return None
return item
class ChessScore:
def __init__(self, figures: list[str]):
self._score = 0
for figure in figures:
if figure.upper() in scores.keys():
self._score += scores[figure.upper()]
else:
raise ChessException("This figure does not exist")
def get_score(self):
return self._score
def __lt__(self, other):
return self._score < other.get_score()
def __le__(self, other):
return self._score <= other.get_score()
def __eq__(self, other):
return self._score == other.get_score()
def __ne__(self, other):
return self._score != other.get_score()
def __gt__(self, other):
return self._score > other.get_score()
def __ge__(self, other):
return self._score >= other.get_score()
def __add__(self, other):
return self._score + other.get_score()
def __sub__(self, other):
return self._score - other.get_score()
+ def __int__(self):
+ return self._score

Дуйгу обнови решението на 29.11.2022 16:47 (преди почти 2 години)

chess_piece = ('R', 'N', 'B', 'Q', 'K', 'P')
scores = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
class ChessException(Exception):
def __init__(self, message):
self._message = message
super().__init__(self._message)
def get_chess_board(string_board: str):
chess_board = [[]] # will use - for spare places
n = 0
for char in string_board:
if char != '/':
if char in chess_piece or char.upper() in chess_piece:
chess_board[n].append(char)
elif ord(char) in range(49, 57):
for k in range(int(char)):
chess_board[n].append('-')
else:
raise ChessException("Incorrect input")
else:
if len(chess_board[n]) != 8:
raise ChessException("Incorrect input")
chess_board.append([])
n += 1
if len(chess_board) != 8:
raise ChessException("Incorrect input")
if invalidate_kings(chess_board):
raise ChessException("kings")
if invalid_pawns(chess_board):
raise ChessException("pawns")
return chess_board
def invalidate_kings(chess_board):
king_positions = []
for i in range(len(chess_board)):
for j in range(len(chess_board[i])):
if chess_board[i][j] == 'K' or chess_board[i][j] == 'k':
if king_positions and king_positions[0][2] == chess_board[i][j]:
raise ChessException("kings")
else:
king_positions.append([i, j, chess_board[i][j]])
if len(king_positions) != 2:
raise ChessException("kings")
# first king -[king_positions[0][0], king_positions[0][1]] --> i, j
# second king -[king_positions[1][0], king_positions[1][1]] --> i1, j1
# invalid --> (i = i1, j1 = j +-1) (i1 = i + 1, j1 = j +-1) (i1 = i - 1, j1 = j+-1) (j = j1, i1 = i +-1
close = ((king_positions[0][0] == king_positions[1][0] or king_positions[1][0] == king_positions[0][0] + 1 or
king_positions[1][0] == king_positions[0][0] - 1)
and (king_positions[1][1] == king_positions[0][1] - 1 or king_positions[1][1] == king_positions[0][1] + 1))
closer = ((king_positions[1][1] == king_positions[0][1]) and (king_positions[0][0] == king_positions[1][0] - 1 or
king_positions[0][0] == king_positions[1][0] + 1))
return close or closer
def invalid_pawns(chess_board):
return 'p' in chess_board[0] + chess_board[7] or 'P' in chess_board[0] + chess_board[7]
class ChessPosition:
def __init__(self, fen: str):
- my_fen = fen.split()
- self._chess_board = get_chess_board(my_fen[0])
+ self._fen = fen.split()
+ self._chess_board = get_chess_board(self._fen[0])
self._white_score = 0
self._black_score = 0
+ self._figures_count = 0
for row in self._chess_board:
for element in row:
if element in chess_piece:
self._white_score += scores[element]
+ self._figures_count += 1
elif ord(element) > 96 and element.upper() in chess_piece:
self._black_score += scores[element.upper()]
+ self._figures_count += 1
def get_white_score(self):
return self._white_score
def get_black_score(self):
return self._black_score
def white_is_winning(self):
return self._white_score > self._black_score
def black_is_winning(self):
return self._black_score > self._white_score
def is_equal(self):
return self._white_score == self._black_score
def __getitem__(self, key):
item = self._chess_board[8 - int(key[1])][ord(key[0]) - ord('A')]
if item == '-':
return None
return item
+
+ def __len__(self):
+ return self._figures_count
+
+ def __str__(self):
+ return self._fen[0]
class ChessScore:
def __init__(self, figures: list[str]):
self._score = 0
for figure in figures:
if figure.upper() in scores.keys():
self._score += scores[figure.upper()]
else:
raise ChessException("This figure does not exist")
def get_score(self):
return self._score
def __lt__(self, other):
return self._score < other.get_score()
def __le__(self, other):
return self._score <= other.get_score()
def __eq__(self, other):
return self._score == other.get_score()
def __ne__(self, other):
return self._score != other.get_score()
def __gt__(self, other):
return self._score > other.get_score()
def __ge__(self, other):
return self._score >= other.get_score()
def __add__(self, other):
return self._score + other.get_score()
def __sub__(self, other):
return self._score - other.get_score()
def __int__(self):
return self._score