Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First: some comprehensions, bool as counter and separate is_safe function solution in Clear category for Pawn Brotherhood by leggewie
def is_safe(pawns_indexes: set, row: int, col: int) -> bool:
'''
return True if pawn at (row,col) is protected by the pawns at pawns_indexes
'''
return ((row - 1, col - 1) in pawns_indexes) or ((row - 1, col + 1) in pawns_indexes)
def safe_pawns(pawns: set) -> int:
'''
count the number of safe pawns
'''
#set up array with pawns' coordinates via set comprehension
pawns_indexes = {(int(p[1])-1, ord(p[0])-97) for p in pawns}
count = sum([int(is_safe(pawns_indexes,row,col)) for row, col in pawns_indexes])
return count
May 31, 2021