【checkio】the Moore neighbourhood

来源:互联网 发布:初中语文软件 编辑:程序博客网 时间:2024/05/17 23:39

早上起来就开始做这道题 顺便学了下python二维数组的东西 主要就是确定边界 还有 好像没什么了

row 和 col 弄反了一直报错越界 也是逗了

觉得 还是慢慢来吧

代码:

def count_neighbours(tuples, the_row,the_col):
    rows = len(tuples)
    cols = len(tuples[0])
    count = 0
    for j in range(the_row-1,the_row+2):
        for k in range(the_col-1,the_col+2):
            if j>=0 and k>=0 and j<rows and k<cols:
                if tuples[j][k] == 1 :
                    count+=1
    if tuples[the_row][the_col] == 1:
        count-=1
    return count


0 0