python-checkio/Home/pawn-brotherhood.py

22 lines
935 B
Python
Raw Normal View History

2019-12-23 17:19:33 +01:00
def safe_pawns(pawns: set) -> int:
cols = "abcdefgh"
safe_count = 0
for pawn in pawns:
row = int(pawn[1])
if row == 1:
continue
col_index = cols.index(pawn[0])
protector_1 = cols[col_index - 1] + str(row - 1) if col_index - 1 >= 0 else None
protector_2 = cols[col_index + 1] + str(row - 1) if col_index + 1 < len(pawns) and row > 1 else None
if protector_1 in pawns or protector_2 in pawns:
safe_count += 1
return safe_count
if __name__ == '__main__':
# These "asserts" using only for self-checking and not necessary for auto-testing
assert safe_pawns({"b4", "d4", "f4", "c3", "e3", "g5", "d2"}) == 6
assert safe_pawns({"b4", "c4", "d4", "e4", "f4", "g4", "e5"}) == 1
assert safe_pawns({"a1", "b2", "c3", "d4", "e5", "f6", "g7", "h8"}) == 8
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")