Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
First solution in Clear category for Pawn Brotherhood by HubertDolny
# migrated from python 2.7
def safe_pawns(pawns):
matrix= []
pawns=list(pawns)
for i in range(0,8):
matrix.append([False,False,False,False,False,False,False,False])
print(matrix)
for i in range(0,len(pawns)):
if pawns[i][0]=="a":
matrix[int(pawns[i][1])-1][0]=True
if pawns[i][0]=="b":
matrix[int(pawns[i][1])-1][1]=True
if pawns[i][0]=="c":
matrix[int(pawns[i][1])-1][2]=True
if pawns[i][0]=="d":
matrix[int(pawns[i][1])-1][3]=True
if pawns[i][0]=="e":
matrix[int(pawns[i][1])-1][4]=True
if pawns[i][0]=="f":
matrix[int(pawns[i][1])-1][5]=True
if pawns[i][0]=="g":
print((999))
matrix[int(pawns[i][1])-1][6]=True
if pawns[i][0]=="h":
print((999))
matrix[int(pawns[i][1])-1][7]=True
count =0
for i in range(0,8):
for j in range(0,8):
if matrix[i][j]==True:
if (i-1)>=0 and (j+1)<8:
if matrix[i][j]==matrix[i-1][j+1]:
count+=1
elif (j-1)>=0:
if matrix[i][j]==matrix[i-1][j-1]:
count+=1
elif (j-1)>=0 and (i-1)>=0:
if matrix[i][j]==matrix[i-1][j-1]:
count+=1
return 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
Nov. 1, 2016