Решение на Шахматни фенове от Николета Бейска

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

Към профила на Николета Бейска

Резултати

  • 8 точки от тестове
  • 0 бонус точки
  • 8 точки общо
  • 14 успешни тест(а)
  • 3 неуспешни тест(а)

Код

class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, fen):
self._rows = fen.split("/")
self._fen = fen
for ind, row in enumerate(self._rows):
for i, ch in enumerate(row):
if ch.isdigit():
count = int(ch)
while count > 0:
row = row[:i] + "-" + row[i+1:]
count -= 1
i += 1
self._rows[ind] = row
for ind, row in enumerate(self._rows):
if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
raise ChessException("kings")
elif "K" in row.upper() and ind + 1 < len(self._rows) and "K" in self._rows[ind + 1].upper():
if abs(row.upper().index("K") - self._rows[ind + 1].upper().index("K")) in (0, 1):
raise ChessException("kings")
if self._fen.count("k") != 1 or self._fen.count("K") != 1:
raise ChessException("kings")
if any(x in ("p", "P") for x in (self._rows[7] + self._rows[0])):
raise ChessException("pawns")
def get_white_score(self):
upper_chars = ''.join(char for char in self._fen if char.isupper())
return ChessScore(list(upper_chars))
def get_black_score(self):
lower_chars = ''.join(char for char in self._fen if char.islower())
return ChessScore(list(lower_chars))
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_black_score() > self.get_white_score()
def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __len__(self):
count = 0
for character in self._fen:
if character.isalpha():
count = count + 1
return count
def __str__(self):
return self._fen
def __getitem__(self, index):
item = self._rows[8-int(index[1])][(ord(index[0].upper()) - ord("A"))]
if item.isalpha():
return item
return None
class ChessScore:
points_for_figures = {"r": 5, "n": 3, "b": 3, "q": 9, "k": 4, "p": 1}
def __init__(self, figures):
self._figures = figures
self.points = 0
for figure in self._figures:
self.points += self.points_for_figures[figure.lower()]
def __int__(self):
return self.points
def __eq__(self, other):
return self.points == other.points
def __le__(self, other):
return self.points <= other.points
def __lt__(self, other):
return self.points < other.points
def __ne__(self, other):
return self.points != other.points
def __gt__(self, other):
return self.points > other.points
def __ge__(self, other):
return self.points >= other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return self.points - other.points

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

.E...E...E.......
======================================================================
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: 8/8/8/3kK3/8/8/8/8

======================================================================
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_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
Exception: No exception raised on: 7p/8/k7/8/7K/8/8/8

----------------------------------------------------------------------
Ran 17 tests in 0.170s

FAILED (errors=3)

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

Николета обнови решението на 28.11.2022 14:14 (преди почти 2 години)

+class ChessException(Exception):
+ pass
+
+
+class ChessPosition:
+
+ @staticmethod
+ def find_count_letters_before_letter(letter, s):
+ i = 0
+ count = 0
+ while s[i] != letter:
+ if s[i].isdigit():
+ count += int(s[i]) - 1
+ i += 1
+ return count
+
+ def __init__(self, fen):
+ self.rows = fen.split("/")
+ self.fen = fen
+ buf = {}
+ for i, row in enumerate(self.rows):
+ if "K" in row.upper():
+ buf[i] = self.find_count_letters_before_letter("K", row.upper())
+
+ for ind, row in enumerate(self.rows):
+ if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
+ raise ChessException("kings")
+ elif "K" in row.upper() and ind + 1 < len(self.rows) and "K" in self.rows[ind + 1].upper():
+ if abs((row.upper().index("K") + buf[ind]) - (self.rows[ind + 1].upper().index("K") + buf[ind+1])) in (0, 1):
+ raise ChessException("kings")
+
+ if self.fen.count("k") != 1 or self.fen.count("K") != 1:
+ raise ChessException("kings")
+ if any(x in ("p", "P") for x in (self.rows[7] + self.rows[0])):
+ raise ChessException("pawns")
+
+ def get_white_score(self):
+ lower_chars = ''.join(char for char in self.fen if char.islower() and char.isalpha())
+ letters = [x for x in lower_chars]
+ return ChessScore(letters)
+
+ def get_black_score(self):
+ upper_chars = ''.join(char for char in self.fen if char.isupper() and char.isalpha())
+ letters = [x for x in upper_chars]
+ return ChessScore(letters)
+
+ def white_is_winning(self):
+ return self.get_white_score() > self.get_black_score()
+
+ def black_is_winning(self):
+ return self.get_black_score() > self.get_white_score()
+
+ def is_equal(self):
+ return self.get_black_score() == self.get_white_score()
+
+ def __len__(self):
+ count = 0
+ for character in self.fen:
+ if character.isalpha():
+ count = count + 1
+ return count
+
+ def __print__(self):
+ print(self.fen)
+
+ def __getitem__(self, index):
+ col = ord(index[0]) - ord("A")
+ row = self.rows[8-int(index[1])]
+ j = 0
+ while col > 0 and j < len(row):
+ if row[j].isdigit():
+ col -= int(row[j])
+ else:
+ col -= 1
+ j += 1
+
+ if row[j-1].isaplha():
+ return row[j-1]
+ return None
+
+
+
+points_for_figures = {"r":5, "n":3, "b":3, "q":9, "k":4, "p":1}
+
+
+class ChessScore:
+
+ def __init__(self, figures):
+ self.figures = figures
+ self.points = 0
+ for figure in self.figures:
+ self.points += points_for_figures[figure.lower()]
+
+ def __int__(self):
+ return self.points
+
+ def __eq__(self, other):
+ return self.points == other.points
+
+ def __le__(self, other):
+ return self.points <= other.points
+
+ def __lt__(self, other):
+ return self.points < other.points
+
+ def __ne__(self, other):
+ return self.points != other.points
+
+ def __gt__(self, other):
+ return self.points > other.points
+
+ def __ge__(self, other):
+ return self.points >= other.points
+
+ def __add__(self, other):
+ return self.points + other.points
+
+ def __sub__(self, other):
+ return self.points - other.points

Николета обнови решението на 28.11.2022 14:23 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessPosition:
@staticmethod
def find_count_letters_before_letter(letter, s):
i = 0
count = 0
while s[i] != letter:
if s[i].isdigit():
count += int(s[i]) - 1
i += 1
return count
def __init__(self, fen):
self.rows = fen.split("/")
self.fen = fen
buf = {}
for i, row in enumerate(self.rows):
if "K" in row.upper():
buf[i] = self.find_count_letters_before_letter("K", row.upper())
for ind, row in enumerate(self.rows):
if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
- raise ChessException("kings")
+ raise ChessException("kings")
elif "K" in row.upper() and ind + 1 < len(self.rows) and "K" in self.rows[ind + 1].upper():
- if abs((row.upper().index("K") + buf[ind]) - (self.rows[ind + 1].upper().index("K") + buf[ind+1])) in (0, 1):
- raise ChessException("kings")
+ if abs((row.upper().index("K") + buf[ind]) - (self.rows[ind + 1].upper().index("K") +
+ buf[ind+1])) in (0, 1):
+ raise ChessException("kings")
if self.fen.count("k") != 1 or self.fen.count("K") != 1:
raise ChessException("kings")
- if any(x in ("p", "P") for x in (self.rows[7] + self.rows[0])):
+ if any(x in ("p", "P") for x in (self.rows[7] + self.rows[0])): #???
raise ChessException("pawns")
def get_white_score(self):
lower_chars = ''.join(char for char in self.fen if char.islower() and char.isalpha())
letters = [x for x in lower_chars]
return ChessScore(letters)
def get_black_score(self):
upper_chars = ''.join(char for char in self.fen if char.isupper() and char.isalpha())
letters = [x for x in upper_chars]
return ChessScore(letters)
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_black_score() > self.get_white_score()
def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __len__(self):
count = 0
for character in self.fen:
if character.isalpha():
count = count + 1
return count
def __print__(self):
print(self.fen)
def __getitem__(self, index):
col = ord(index[0]) - ord("A")
row = self.rows[8-int(index[1])]
j = 0
while col > 0 and j < len(row):
if row[j].isdigit():
col -= int(row[j])
else:
col -= 1
j += 1
if row[j-1].isaplha():
return row[j-1]
return None
points_for_figures = {"r":5, "n":3, "b":3, "q":9, "k":4, "p":1}
class ChessScore:
def __init__(self, figures):
self.figures = figures
self.points = 0
for figure in self.figures:
self.points += points_for_figures[figure.lower()]
def __int__(self):
return self.points
def __eq__(self, other):
return self.points == other.points
def __le__(self, other):
return self.points <= other.points
def __lt__(self, other):
return self.points < other.points
def __ne__(self, other):
return self.points != other.points
def __gt__(self, other):
return self.points > other.points
def __ge__(self, other):
return self.points >= other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return self.points - other.points
+

Николета обнови решението на 28.11.2022 14:27 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessPosition:
@staticmethod
def find_count_letters_before_letter(letter, s):
i = 0
count = 0
while s[i] != letter:
if s[i].isdigit():
count += int(s[i]) - 1
i += 1
return count
def __init__(self, fen):
- self.rows = fen.split("/")
- self.fen = fen
+ self._rows = fen.split("/")
+ self._fen = fen
buf = {}
- for i, row in enumerate(self.rows):
+ for i, row in enumerate(self._rows):

Бих ти препоръчал да преправиш цифрите от редовете (т.е. празните полета) в символи преди да се втурнеш да валидираш. Ще спестиш доста сложни операции.

if "K" in row.upper():
buf[i] = self.find_count_letters_before_letter("K", row.upper())
- for ind, row in enumerate(self.rows):
+ for ind, row in enumerate(self._rows):
if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
raise ChessException("kings")
- elif "K" in row.upper() and ind + 1 < len(self.rows) and "K" in self.rows[ind + 1].upper():
- if abs((row.upper().index("K") + buf[ind]) - (self.rows[ind + 1].upper().index("K") +
+ elif "K" in row.upper() and ind + 1 < len(self._rows) and "K" in self._rows[ind + 1].upper():
+ if abs((row.upper().index("K") + buf[ind]) - (self._rows[ind + 1].upper().index("K") +
buf[ind+1])) in (0, 1):
raise ChessException("kings")
- if self.fen.count("k") != 1 or self.fen.count("K") != 1:
+ if self._fen.count("k") != 1 or self._fen.count("K") != 1:
raise ChessException("kings")
- if any(x in ("p", "P") for x in (self.rows[7] + self.rows[0])): #???
+ if any(x in ("p", "P") for x in (self._rows[7] + self._rows[0])):
raise ChessException("pawns")
def get_white_score(self):
- lower_chars = ''.join(char for char in self.fen if char.islower() and char.isalpha())
+ lower_chars = ''.join(char for char in self._fen if char.islower() and char.isalpha())
letters = [x for x in lower_chars]
return ChessScore(letters)
def get_black_score(self):
- upper_chars = ''.join(char for char in self.fen if char.isupper() and char.isalpha())
+ upper_chars = ''.join(char for char in self._fen if char.isupper() and char.isalpha())
letters = [x for x in upper_chars]
return ChessScore(letters)
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_black_score() > self.get_white_score()
def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __len__(self):
count = 0
- for character in self.fen:
+ for character in self._fen:
if character.isalpha():
count = count + 1
return count
def __print__(self):
- print(self.fen)
+ print(self._fen)
def __getitem__(self, index):
col = ord(index[0]) - ord("A")
- row = self.rows[8-int(index[1])]
+ row = self._rows[8-int(index[1])]
j = 0
while col > 0 and j < len(row):
if row[j].isdigit():
col -= int(row[j])
else:
col -= 1
j += 1
if row[j-1].isaplha():
return row[j-1]
return None
points_for_figures = {"r":5, "n":3, "b":3, "q":9, "k":4, "p":1}
class ChessScore:
def __init__(self, figures):
- self.figures = figures
- self.points = 0
- for figure in self.figures:
- self.points += points_for_figures[figure.lower()]
+ self._figures = figures
+ self._points = 0
+ for figure in self._figures:
+ self._points += points_for_figures[figure.lower()]
def __int__(self):
- return self.points
+ return self._points
def __eq__(self, other):
- return self.points == other.points
+ return self._points == other._points
def __le__(self, other):
- return self.points <= other.points
+ return self._points <= other._points
def __lt__(self, other):
- return self.points < other.points
+ return self._points < other._points
def __ne__(self, other):
- return self.points != other.points
+ return self._points != other._points
def __gt__(self, other):
- return self.points > other.points
+ return self._points > other._points
def __ge__(self, other):
- return self.points >= other.points
+ return self._points >= other._points
def __add__(self, other):
- return self.points + other.points
+ return self._points + other._points
def __sub__(self, other):
- return self.points - other.points
+ return self._points - other._points

Николета обнови решението на 28.11.2022 23:38 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessPosition:
- @staticmethod
- def find_count_letters_before_letter(letter, s):
- i = 0
- count = 0
- while s[i] != letter:
- if s[i].isdigit():
- count += int(s[i]) - 1
- i += 1
- return count
-
def __init__(self, fen):
self._rows = fen.split("/")
self._fen = fen
- buf = {}
- for i, row in enumerate(self._rows):
- if "K" in row.upper():
- buf[i] = self.find_count_letters_before_letter("K", row.upper())
+ for row in self._rows:
+ for i, ch in enumerate(row):
+ if ch.isdigit():
+ count = int(ch)
+ while count > 0:
+ row = row[:i] + "-" + row[i+1:]
+ count -= 1
+ i += 1
for ind, row in enumerate(self._rows):
if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
raise ChessException("kings")
elif "K" in row.upper() and ind + 1 < len(self._rows) and "K" in self._rows[ind + 1].upper():
- if abs((row.upper().index("K") + buf[ind]) - (self._rows[ind + 1].upper().index("K") +
- buf[ind+1])) in (0, 1):
+ if abs(row.upper().index("K") - self._rows[ind + 1].upper().index("K")) in (0, 1):
raise ChessException("kings")
if self._fen.count("k") != 1 or self._fen.count("K") != 1:
raise ChessException("kings")
- if any(x in ("p", "P") for x in (self._rows[7] + self._rows[0])):
+ if any(x in ("p", "P") for x in (self._rows[7] + self._rows[0])):
raise ChessException("pawns")
def get_white_score(self):
- lower_chars = ''.join(char for char in self._fen if char.islower() and char.isalpha())
- letters = [x for x in lower_chars]
- return ChessScore(letters)
+ lower_chars = ''.join(char for char in self._fen if char.islower())
+ return ChessScore(list(lower_chars))
def get_black_score(self):
- upper_chars = ''.join(char for char in self._fen if char.isupper() and char.isalpha())
- letters = [x for x in upper_chars]
- return ChessScore(letters)
+ upper_chars = ''.join(char for char in self._fen if char.isupper())
+ return ChessScore(list(upper_chars))
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_black_score() > self.get_white_score()
def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __len__(self):
count = 0
for character in self._fen:
if character.isalpha():
count = count + 1
return count
- def __print__(self):
- print(self._fen)
+ def __str__(self):
+ return self._fen
def __getitem__(self, index):
- col = ord(index[0]) - ord("A")
- row = self._rows[8-int(index[1])]
- j = 0
- while col > 0 and j < len(row):
- if row[j].isdigit():
- col -= int(row[j])
- else:
- col -= 1
- j += 1
-
- if row[j-1].isaplha():
- return row[j-1]
+ item = self._rows[8-int(index[1])][(ord(index[0]) - ord("A"))]
+ if item.isalpha():
+ return item
return None
-
-points_for_figures = {"r":5, "n":3, "b":3, "q":9, "k":4, "p":1}
-
-
class ChessScore:
+ points_for_figures = {"r": 5, "n": 3, "b": 3, "q": 9, "k": 4, "p": 1}
+
def __init__(self, figures):
self._figures = figures
- self._points = 0
+ self.points = 0
for figure in self._figures:
- self._points += points_for_figures[figure.lower()]
+ self.points += self.points_for_figures[figure.lower()]
def __int__(self):
- return self._points
+ return self.points
def __eq__(self, other):
- return self._points == other._points
+ return self.points == other.points
def __le__(self, other):
- return self._points <= other._points
+ return self.points <= other.points
def __lt__(self, other):
- return self._points < other._points
+ return self.points < other.points
def __ne__(self, other):
- return self._points != other._points
+ return self.points != other.points
def __gt__(self, other):
- return self._points > other._points
+ return self.points > other.points
def __ge__(self, other):
- return self._points >= other._points
+ return self.points >= other.points
def __add__(self, other):
- return self._points + other._points
+ return self.points + other.points
def __sub__(self, other):
- return self._points - other._points
-
+ return self.points - other.points

Николета обнови решението на 29.11.2022 14:44 (преди почти 2 години)

class ChessException(Exception):
pass
class ChessPosition:
def __init__(self, fen):
self._rows = fen.split("/")
self._fen = fen
- for row in self._rows:
+ for ind, row in enumerate(self._rows):
for i, ch in enumerate(row):
if ch.isdigit():
count = int(ch)
while count > 0:
row = row[:i] + "-" + row[i+1:]
count -= 1
i += 1
+ self._rows[ind] = row
for ind, row in enumerate(self._rows):
if "k" in row and "K" in row and abs(row.index("k") - row.index("K")) == 1:
raise ChessException("kings")
elif "K" in row.upper() and ind + 1 < len(self._rows) and "K" in self._rows[ind + 1].upper():
if abs(row.upper().index("K") - self._rows[ind + 1].upper().index("K")) in (0, 1):
raise ChessException("kings")
if self._fen.count("k") != 1 or self._fen.count("K") != 1:
raise ChessException("kings")
if any(x in ("p", "P") for x in (self._rows[7] + self._rows[0])):
raise ChessException("pawns")
def get_white_score(self):
- lower_chars = ''.join(char for char in self._fen if char.islower())
- return ChessScore(list(lower_chars))
-
- def get_black_score(self):
upper_chars = ''.join(char for char in self._fen if char.isupper())
return ChessScore(list(upper_chars))
+ def get_black_score(self):
+ lower_chars = ''.join(char for char in self._fen if char.islower())
+ return ChessScore(list(lower_chars))
+
def white_is_winning(self):
return self.get_white_score() > self.get_black_score()
def black_is_winning(self):
return self.get_black_score() > self.get_white_score()
def is_equal(self):
return self.get_black_score() == self.get_white_score()
def __len__(self):
count = 0
for character in self._fen:
if character.isalpha():
count = count + 1
return count
def __str__(self):
return self._fen
- def __getitem__(self, index):
- item = self._rows[8-int(index[1])][(ord(index[0]) - ord("A"))]
+ def __getitem__(self, index):
+ item = self._rows[8-int(index[1])][(ord(index[0].upper()) - ord("A"))]
if item.isalpha():
return item
return None
class ChessScore:
points_for_figures = {"r": 5, "n": 3, "b": 3, "q": 9, "k": 4, "p": 1}
def __init__(self, figures):
self._figures = figures
self.points = 0
for figure in self._figures:
self.points += self.points_for_figures[figure.lower()]
def __int__(self):
return self.points
def __eq__(self, other):
return self.points == other.points
def __le__(self, other):
return self.points <= other.points
def __lt__(self, other):
return self.points < other.points
def __ne__(self, other):
return self.points != other.points
def __gt__(self, other):
return self.points > other.points
def __ge__(self, other):
return self.points >= other.points
def __add__(self, other):
return self.points + other.points
def __sub__(self, other):
return self.points - other.points
+