Not sure if bug or I'm doing something wrong
I would like to give some feedback about ...
From: http://www.checkio.org/mission/count-neighbours/solve/
I'm somewhat of a newbie when it comes to python and coding in general so please bear with me. I have a problem with the second example, When I try to run it via try it, it gives the ff output:
>>> count_neighbours(((1, 0, 0, 1, 0), (0, 1, 0, 0, 0), (0, 0, 1, 0, 1), (1, 0, 0, 0, 0), (0, 0, 1, 0, 0),), 0, 0) <<< 1
but when I try to run my code via run and check:
AssertionError: 2nd example <module>, 133
It also gives me an answer of 4.
Below is my code if you need it:
HTTP_USER_AGENT:
Mozilla/5.0 (Windows NT 6.1; WOW64; rv:32.0) Gecko/20100101 Firefox/32.0
hasher = 0
def call_upper_left(grid, row, col):
global hasher
if (grid[row - 1][col -1] == 1):
hasher +=1
def call_upper_mid(grid, row, col):
global hasher
if (grid[row - 1][col] == 1):
hasher += 1
def call_upper_right(grid, row, col):
global hasher
if (grid[row - 1][col + 1] == 1):
hasher += 1
def call_left(grid, row, col):
global hasher
if (grid[row][col -1] == 1):
hasher += 1
def call_right(grid, row, col):
global hasher
if (grid[row][col + 1] == 1):
hasher += 1
def call_lower_left(grid, row, col):
global hasher
if (grid[row + 1][col -1] == 1):
hasher += 1
def call_lower_mid(grid, row, col):
global hasher
if (grid[row + 1][col] == 1):
hasher += 1
def call_lower_right(grid, row, col):
global hasher
if (grid[row + 1][col + 1] == 1):
hasher += 1
def count_neighbours(grid, row, col):
global hasher
# determine x and y position
#start with corners
#corner upper left
if (row == 0 and col == 0):
call_right(grid, row, col)
call_lower_mid(grid, row, col)
call_lower_right(grid, row, col)
return hasher
#corner upper right
elif(row == 0 and col == len(grid) - 1):
call_left(grid, row, col)
call_lower_left(grid, row, col)
call_lower_mid(grid, row, col)
return hasher
#corner lower left
elif(row == len(grid) - 1 and col == 0):
call_upper_mid(grid, row, col)
call_upper_right(grid, row, col)
call_right(grid, row, col)
return hasher
#corner lower right
elif(row == len(grid) - 1 and col == len(grid) - 1):
call_upper_left(grid, row, col)
call_upper_mid(grid, row, col)
call_left(grid, row, col)
return hasher
#go for the sides
#left
elif(row != 0 or row != len(grid) - 1)and(col==0):
call_upper_mid(grid, row, col)
call_upper_right(grid, row, col)
call_right(grid, row, col)
call_lower_mid(grid, row, col)
call_lower_right(grid, row, col)
return hasher
#right
elif(row != 0 or row != len(grid) - 1) and (col == len(grid) - 1 ):
call_upper_left(grid, row, col)
call_upper_mid(grid, row, col)
call_left(grid, row, col)
call_lower_left(grid, row, col)
call_lower_mid(grid, row, col)
return hasher
#aim for the top
elif (row == 0) and (col != 0 or col != len(grid) - 1):
call_left(grid, row, col)
call_right(grid, row, col)
call_lower_left(grid, row, col)
call_lower_mid(grid, row, col)
call_lower_right(grid, row, col)
return hasher
#and kamikaze to rock bottom
elif (row == len(grid) - 1) and (col != 0 or col != len(grid) - 1):
call_upper_left(grid, row, col)
call_upper_mid(grid, row, col)
call_upper_right(grid, row, col)
call_left(grid, row, col)
call_right(grid, row, col)
return hasher
#and go all out
else:
call_upper_left(grid, row, col)
call_upper_mid(grid, row, col)
call_upper_right(grid, row, col)
call_left(grid, row, col)
call_right(grid, row, col)
call_lower_left(grid, row, col)
call_lower_mid(grid, row, col)
call_lower_right(grid, row, col)
return hasher