我想问一下 - 如何比较:
这是一个练习 - 计算海上岛屿的陆地周长(陆地='X',海='O')。比较垂直线上的两个字符 - 在我看来 - 需要计算特定土地平方的边界数量。
**编辑:练习链接: https://www.codewars.com/kata/land-perimeter/train/python
我认为zip()函数可以提供帮助,但我不知道/没有找到 - 如何将它实现为for循环..
下面的代码(首先考虑/草案,PrevHor,prevVer,p代码 - 现在不重要):
island = ["OXOOOX", "OXOXOO", "XXOOOX", "OXXXOO", "OOXOOX", "OXOOOO", "OOXOOX", "OOXOOO", "OXOOOO", "OXOOXX"]
def land(island):
prevHor = 0
prevVer = 0
p = 0
for i, j in enumerate(island):
print(i)
for k, l in enumerate(j):
print(j)
print(k)
if l == 'X' and prevHor == 0 and prevVer == 0:
prevHor = 1
prevVer = 1
if l =='X' and prevHor == 1 and prevVer == 1:
prevHor = 2
if l == 'X' and i > 0 and l[k] in j[i-1] == 'X':
print('Vertical Match!!')
答案 0 :(得分:0)
任何人..,请.. :) 我收到了一个错误:
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
land(island)
File "<pyshell#2>", line 17, in land
if l == 'X' and i > 0 and l[k] in j[i-1] == 'X':
IndexError: string index out of range