Why does it give me IndexError: list index out of range
in line 28 if (cells[y][x] % TOP == 0
?
If i remove if __name__ == '__main__':
everything works perfectly.
Count Connectivity Components is a Kata on codewars, if interested.
TOP, RIGHT, BOTTOM, LEFT = 2, 3, 5, 7
def components(grid):
grid = grid.split('\n')
result = bfs(set_directions(grid))
d = {}
for k in result:
if not k in d:
d[k] = 0
d[k] += 1
result = []
for k in d:
result.append((k, d[k]))
result.sort(reverse=True)
return result
def bfs(cells):
queue = [(0,0)]
marked = set()
result = []
node_count = 0
while True:
x,y = queue.pop(0)
marked.add((x,y))
node_count += 1
if (cells[y][x] % TOP == 0
and (x,y-1) not in marked
and (x,y-1) not in queue):
queue.append((x,y-1))
if (cells[y][x] % BOTTOM == 0
and (x,y+1) not in marked
and (x,y+1) not in queue):
queue.append((x,y+1))
if (cells[y][x] % LEFT == 0
and (x-1,y) not in marked
and (x-1,y) not in queue):
queue.append((x-1,y))
if (cells[y][x] % RIGHT == 0
and (x+1,y) not in marked
and (x+1,y) not in queue):
queue.append((x+1,y))
cells[y][x] = 'm'
if not queue:
result.append(node_count)
node_count = 0
flag = False
for y in range(len(cells)):
for x in range(len(cells[y])):
if cells[y][x] != 'm':
queue.append((x,y))
flag = True
break
if flag:
break
else:
break
return result
def set_directions(grid):
cells = \
[[1 for i in range(grid[0].count('+')-1)]
for r in range(len(grid)//2)]
for r in range(1,len(grid), 2):
for i in range(0, len(grid[r])-1, 3):
subject = grid[r][i:i+3]
if subject == ' ':
cells[r//2][i//3] *= LEFT
for i in range(3, len(grid[r]), 3):
subject = grid[r][i-2:i+1]
if subject == ' ':
cells[r//2][i//3-1] *= RIGHT
for c in range(1,len(grid[0]), 3):
column = ''.join([r[c] for r in grid])
for i in range(0,len(grid)-1, 2):
subject = column[i:i+2]
if subject == ' ':
cells[i//2][c//3] *= TOP
for i in range(1,len(grid),2):
subject = column[i:i+2]
if subject == ' ':
cells[i//2][c//3] *= BOTTOM
return cells
if __name__ == '__main__':
a = '''\
+--+--+--+
| | |
+--+ + +
| | |
+ +--+--+
| | |
+--+--+--+'''
print(a)
print(components(a))
After doing some testing, I noticed that the a
string is not the same in both cases (With the if __name__ == "__main__"
and without it). The problem is that when you use if __name__ == "__main__"
you indent the a
string once to put it inside the if statement. It might seem like you indented the code block, but you actually put spaces inside the string itself.
With the if __name__ == "__main__"
:
[' +--+--+--+', ' | | |', ' +--+ + +', ' | | |', ' + +--+--+', ' | | |', ' +--+--+--+']
Without the if __name__ == "__main__"
:
['+--+--+--+', '| | |', '+--+ + +', '| | |', '+ +--+--+', '| | |', '+--+--+--+']
To fix this simply remove the spaces like so:
if __name__ == '__main__':
a = '''\
+--+--+--+
| | |
+--+ + +
| | |
+ +--+--+
| | |
+--+--+--+'''
print(components(a))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.