Решение на Шахматни фенове от Валерия Стояновска

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

Към профила на Валерия Стояновска

Резултати

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

Код

white_figures_dict = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
black_figures_dict = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
class ChessException:
pass
class ChessPosition(ChessException):

Вече видно от лога с тестовете, но няма логика това да наследява ChessException.
Препоръчвам да разгледаш останалите решения. Ако има нещо неясно, питай да разясним.

def __init__(self, figures):
self.figures = figures
self.white_figures = []
self.black_figures = []
if figures in white_figures_dict.keys():
self.white_figures.append(figures)
elif figures in black_figures_dict.keys():
self.black_figures.append(figures)
counter_K = 0
counter_k = 0
for white in white_figures_dict:
if white == 'K':
counter_K += 1
for black in black_figures_dict:
if black == 'k':
counter_k += 1
if counter_k > 1 or counter_K > 1:
raise Exception('kings')
def get_white_score(self):
# white_score = 0
# for figure in self.white_figures:
# if score == 'R':
# white_score += 5
# elif figure in ('N', 'B'):
# white_score += 3
# elif figure == 'Q':
# white_score += 9
# elif figure == 'K':
# white_score += 4
# else:
# white_score += 1
# return white_score
score = ChessScore(self.white_figures)
return score.int()
def get_black_score(self):
# black_score = 0
# for figure in self.black_figures:
# if figure == 'r':
# black_score += 5
# elif figure in ('n', 'b'):
# black_score += 3
# elif figure == 'q':
# black_score += 9
# elif figure == 'k':
# black_score += 4
# else:
# black_score += 1
# return black_score
score = ChessScore(self.black_figures)
return score.int()
def white_is_winning(self):
white_score = self.get_white_score()
black_score = self.get_black_score()
if white_score > black_score:
return True
else:
return False
def black_is_winning(self):
white_score = self.get_white_score()
black_score = self.get_black_score()
if black_score > white_score:
return True
else:
return False
def is_equal(self):
white_score = self.get_white_score()
black_score = self.get_black_score()
if white_score == black_score:
return True
else:
return False
class ChessScore:
def __init__(self, list_figures):
if list_figures in (white_figures_dict.keys(), black_figures_dict.keys()):

Това няма да работи така, както очакваш. Проверяваш дали list_figures е в колекция, но колекцията ще съдържа точно два елемента - двата списъка с ключове. Реално те дори не са списъци, а dict_keys обекти. Това, което ти трябва, е по-скоро list_figures in list(white_figures_dict.keys()) + black_figures_dict.keys())

self.list_figures = list_figures.lower()
def __lt__(self, other): # a < b
return self.list_figures < other.list_figures
def __gt__(self, other): # a > b
return self.list_figures > other.list_figures
def __le__(self, other): # a <= b
return self.list_figures <= other.list_figures
def __ge__(self, other): # a >= b
return self.list_figures >= other.list_figures
def __eq__(self, other): # a == b
return self.list_figures == other.list_figures
def __ne__(self, other): # a != b
return self.list_figures != other.list_figures
def __add__(self, other): # a + b
return ChessScore(self.list_figures + other.list_figures)
def int(self):
sum = 0
for figure in self.list_figures:
if figure == 'r':
sum += 5
elif figure in ('n', 'b'):
sum += 3
elif figure == 'q':
sum += 9
elif figure == 'k':
sum += 4
else:
sum += 1
return sum

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

EEEEEEEEEEFEEEEEE
======================================================================
ERROR: test_correct_parent (test.TestChessException)
Ensure ChessException is a child of Exception.
----------------------------------------------------------------------
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
TypeError: ChessException() takes no arguments

======================================================================
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

======================================================================
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
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
TypeError: 'ChessPosition' object is not subscriptable

======================================================================
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

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

======================================================================
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
TypeError: object of type 'ChessPosition' has no len()

======================================================================
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: p7/8/k7/8/7K/8/8/8

======================================================================
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
Exception: No exception raised on: P7/K7/k7/8/8/8/8/8

======================================================================
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
ERROR: 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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
ERROR: test_comparison (test.TestChessScore)
Test correct comparison on a pair of scores.
----------------------------------------------------------------------
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
AttributeError: 'ChessScore' object has no attribute 'list_figures'

======================================================================
ERROR: test_correct_mapping_of_pieces (test.TestChessScore)
Test correct mapping for each piece.
----------------------------------------------------------------------
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
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'ChessScore'

======================================================================
ERROR: test_correct_sum_of_pieces (test.TestChessScore)
Test correct sum for random pieces.
----------------------------------------------------------------------
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
TypeError: int() argument must be a string, a bytes-like object or a real number, not 'ChessScore'

======================================================================
FAIL: 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
AssertionError: '<solution.ChessPosition object at 0xffff8d0f6380>' != '6k1/5p2/6p1/8/7p/8/6PP/6K1'
- <solution.ChessPosition object at 0xffff8d0f6380>
+ 6k1/5p2/6p1/8/7p/8/6PP/6K1


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

FAILED (failures=1, errors=16)

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

Валерия обнови решението на 29.11.2022 16:40 (преди над 1 година)

+white_figures_dict = {'R': 5, 'N': 3, 'B': 3, 'Q': 9, 'K': 4, 'P': 1}
+black_figures_dict = {'r': 5, 'n': 3, 'b': 3, 'q': 9, 'k': 4, 'p': 1}
+
+
+class ChessException:
+ pass
+
+
+class ChessPosition(ChessException):

Вече видно от лога с тестовете, но няма логика това да наследява ChessException.
Препоръчвам да разгледаш останалите решения. Ако има нещо неясно, питай да разясним.

+
+ def __init__(self, figures):
+ self.figures = figures
+ self.white_figures = []
+ self.black_figures = []
+
+ if figures in white_figures_dict.keys():
+ self.white_figures.append(figures)
+ elif figures in black_figures_dict.keys():
+ self.black_figures.append(figures)
+
+ counter_K = 0
+ counter_k = 0
+ for white in white_figures_dict:
+ if white == 'K':
+ counter_K += 1
+
+ for black in black_figures_dict:
+ if black == 'k':
+ counter_k += 1
+
+ if counter_k > 1 or counter_K > 1:
+ raise Exception('kings')
+
+ def get_white_score(self):
+ # white_score = 0
+ # for figure in self.white_figures:
+ # if score == 'R':
+ # white_score += 5
+ # elif figure in ('N', 'B'):
+ # white_score += 3
+ # elif figure == 'Q':
+ # white_score += 9
+ # elif figure == 'K':
+ # white_score += 4
+ # else:
+ # white_score += 1
+ # return white_score
+
+ score = ChessScore(self.white_figures)
+ return score.int()
+
+ def get_black_score(self):
+ # black_score = 0
+ # for figure in self.black_figures:
+ # if figure == 'r':
+ # black_score += 5
+ # elif figure in ('n', 'b'):
+ # black_score += 3
+ # elif figure == 'q':
+ # black_score += 9
+ # elif figure == 'k':
+ # black_score += 4
+ # else:
+ # black_score += 1
+ # return black_score
+
+ score = ChessScore(self.black_figures)
+ return score.int()
+
+ def white_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+
+ if white_score > black_score:
+ return True
+ else:
+ return False
+
+ def black_is_winning(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+
+ if black_score > white_score:
+ return True
+ else:
+ return False
+
+ def is_equal(self):
+ white_score = self.get_white_score()
+ black_score = self.get_black_score()
+
+ if white_score == black_score:
+ return True
+ else:
+ return False
+
+
+class ChessScore:
+ def __init__(self, list_figures):
+ if list_figures in (white_figures_dict.keys(), black_figures_dict.keys()):

Това няма да работи така, както очакваш. Проверяваш дали list_figures е в колекция, но колекцията ще съдържа точно два елемента - двата списъка с ключове. Реално те дори не са списъци, а dict_keys обекти. Това, което ти трябва, е по-скоро list_figures in list(white_figures_dict.keys()) + black_figures_dict.keys())

+ self.list_figures = list_figures.lower()
+
+ def __lt__(self, other): # a < b
+ return self.list_figures < other.list_figures
+
+ def __gt__(self, other): # a > b
+ return self.list_figures > other.list_figures
+
+ def __le__(self, other): # a <= b
+ return self.list_figures <= other.list_figures
+
+ def __ge__(self, other): # a >= b
+ return self.list_figures >= other.list_figures
+
+ def __eq__(self, other): # a == b
+ return self.list_figures == other.list_figures
+
+ def __ne__(self, other): # a != b
+ return self.list_figures != other.list_figures
+
+ def __add__(self, other): # a + b
+ return ChessScore(self.list_figures + other.list_figures)
+
+ def int(self):
+ sum = 0
+
+ for figure in self.list_figures:
+ if figure == 'r':
+ sum += 5
+ elif figure in ('n', 'b'):
+ sum += 3
+ elif figure == 'q':
+ sum += 9
+ elif figure == 'k':
+ sum += 4
+ else:
+ sum += 1
+ return sum