Enable Javascript in your browser and then refresh this page, for a much enhanced experience.
Pawn Brotherhood exercise solution in Clear category for Pawn Brotherhood by Fokzterrier
def safe_pawns(pawns):
pawns_indexes = set()
for p in pawns:
row = int(p[1]) - 1
col = ord(p[0]) - 97
pawns_indexes.add((row, col))
count = 0
for row, col in pawns_indexes:
is_safe = ((row - 1, col - 1) in pawns_indexes) or ((row - 1, col + 1) in pawns_indexes)
if is_safe:
count += 1
return count
# a1 = 1
# a2 = 2
# a3 = 3
# a4 = 4
#a5 = 5
#a6 = 6
# a7 = 7
# a8 = 8
#b1 = 9
# b2 = 10
# b4=12
# b5=13
# b6=14
# b7=15
# b8=16
# c1=17
# c2=18
# c3=19
# c4=20
# c5=21
# c6=22
# c7=23
# c8=24
# d1=26
# d2=26
# d3=27
# d4=28
# d5=29
# d6=30
# d7=31
# d8=32
# e1=33
# e2=34
# e3=35
# e4=36
# e5=37
# e6=38
# e7=39
# e8=40
# f1=41
# f2=42
# f3=43
# f4=44
# f5=45
# f6=46
# f7=47
# f8=48
# g1=49
# g2=50
# g3=51
# g4=52
# g5=53
# g6=54
# g7=55
# g8=56
# h1=57
# h2=58
# h3=59
# h4=60
# h5=61
# h6=62
# h7=63
# h8=64
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
Oct. 26, 2016