Hello,
my Code works for all the assert cases in the main. But there are extra tests (1/1) that didn't pass. It is a edge case on the right side. I don't get output on command line. Help...
From: https://checkio.org/mission/count-neighbours/solve/
HTTP_USER_AGENT:
Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:47.0) Gecko/20100101 Firefox/47.0
def count_neighbours(grid, row, col):
subtract_one = False; left = True; right = True; up = True; down = True; # this section checks, if we have to subtract one # from our result, because we only count neighbours, not the center cell print("grid(row):"+str(grid[row])); check_elem = grid[row]; print("grid(row).elem:" + str(check_elem[col])); check_elem2 = check_elem[col]; if check_elem2 == 1: subtract_one = True; #checking edge cases, marking bools if (col-1) < 0: left = False; print("leftFalse"+str(col-1)); if (col+1) > len(grid[0])-1: right = False; print("rightFalse"+str(col+1)); if (row-1) < 0: up = False; print("upFalse"+str(row-1)); if (row+ 1) > len(grid)-1: down = False; print("downFalse"+str(row+1)); #create a small array, depending on edge cases if up == True and down == True and left == True and right == True: myArray = [[row[col-1] for row in grid], [row[col] for row in grid], [row[col+1] for row in grid]]; print("everythingTrue"); elif up == True and left == True: myArray = [[row[col-1] for row in grid], [row[col] for row in grid]]; print("upleftTrue"); elif up == True and right == True: myArray = [[row[col] for row in grid],[row[col+1] for row in grid]]; print("uprightTrue"); elif down == True and left == True: myArray = [[row[col-1] for row in grid], [row[col] for row in grid]]; print("downleftTrue"); elif down == True and right == True: myArray = [[row[col] for row in grid], [row[col+1] for row in grid]]; print("downrightTrue"); myArray2 = []; myArray3 = []; myArray4 = []; myArray5 = []; print("lenGrid0:"+str(len(grid[0]))); print("lenGrid:" +str(len(grid))); test1 = 0; test2 = 0; test3 = 0; # append elements from small grid to myArray2 # to make it ready for counting for elem in myArray: print("elem:" + str(elem)); if left == True: test1 = elem[row-1]; myArray2.append(test1); print("Test1:"+str(test1)); #this will be added anyway test2 = elem[row]; myArray2.append(test2); print("Test2:"+str(test2)); if right == True: test3 = elem[row+1]; myArray2.append(test3); print("Test3:"+str(test3)); else: print("RightOut"); myArray3.append(myArray2); for x in myArray3: for y in x: myArray4.append(y); print("myArray:" + str(myArray)); print("myArray2:" + str(myArray2)); print("myArray3:" + str(myArray3)); print("myArray4:" + str(myArray4)); # subtract one from result, if center cell == 1 #myArray5.count(1); result = myArray4.count(1); if subtract_one == True and result != 0: result -= 1; print("return:" + str(result)); return result;
if name == 'main': #These "asserts" using only for self-checking and not necessary for auto-testing assert countneighbours(((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),), 1, 2) == 3, "1st example" assert countneighbours(((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, "2nd example" assert countneighbours(((1, 1, 1), (1, 1, 1), (1, 1, 1),), 0, 2) == 3, "Dense corner" assert countneighbours(((0, 0, 0), (0, 1, 0), (0, 0, 0),), 1, 1) == 0, "Single"