Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Second solution in Clear category for Pawn Brotherhood by Red_Ale
def safe_pawns(pawns: set) -> int:
files = "0abcdefgh9" # files is to create the columns check string in if cycle.
# 0 and 9 are added to prevent the error message.
counter = 0
for x in pawns:
alfa = str(int(x[1])-1) # adding alfa to make if cycle more readable.
# alfa finds the rank where should be the defender.
files_pos = files.find(x[0]) # files_pos finds the file position for x element and
# in if cycle it is shifted 1 position left and right.
# the cycle compose the square (as a string) where the defender
# should be. If found in our list, +1 is added.
if (files[files_pos-1] + alfa) in pawns or (files[files_pos+1] + alfa) in pawns:
counter += 1
return counter
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
print("Coding complete? Click 'Check' to review your tests and earn cool rewards!")
May 17, 2021
Comments: