Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pawn Brotherhood by Raknoche
'''''''''
This function uses a list comprehension to build a list of safe pawns
To check if a pawn is safe, we look at the previous row using int(i[1]-1)
and the left and right column using chr(ord(i[0]) +/- 1).
Note: ord() converts a string to a number, and char convert a number to a string.
We don't need to worry about converting ord('a')-1 to a char, for instance, because
a non-alpha char will never appear in the pawns list anyway
If the left diagonal or right diagonal spot is in the pawns list, then the current pawn is safe
'''''''''
def safe_pawns(pawns):
return len([i for i in pawns if (chr(ord(i[0])-1) + str(int(i[1])-1)) in pawns or (chr(ord(i[0])+1) + str(int(i[1])-1)) in pawns])
April 10, 2016
Comments: