Pawn Brotherhood
1 alpha_table = "abcdefgh"
2
3 def safe_pawns(pawns):
4 safe_count = 0
5
6 for spawn in pawns:
7 if spawn[1] == '1':
8 continue
9
10 pos = alpha_table.find(spawn[0])
11 str_row = str(int(spawn[1]) - 1)
12
13 if pos == 0:
14 if (alpha_table[1] + str_row) in pawns:
15 safe_count += 1
16 continue
17
18 if pos == 7:
19 if (alpha_table[6] + str_row) in pawns:
20 safe_count += 1
21 continue
22
23 if (alpha_table[pos - 1] + str_row) in pawns or (alpha_table[pos + 1] + str_row) in pawns:
24 safe_count += 1
25
26 return safe_count
自定义了一个小写字母表, 找出某字母的前后字母, 如b的(a, c)
另外可以使用chr
(
ord
(pawn[
0
])
+
1
),
chr
(
ord
(pawn[
0
])
-
1
)获得前后字母, ord相当于获得字母的数码(如ascii码), chr是逆过程