2

I have a grid as a tuple of tuples with integers (1/0), a row number and column number for a cell as integers. And I have to find how many neighbouring cells have neighbours as an integer.

It's a task from the www.checkio.org, an interesting site of learning python.

Here is my code:

def count_neighbours(grid, row, col):
    grid = ()
    count = 0
    for pos in ((row - 1, col), (row + 1, col), (row, col - 1), (row, col + 1), (row - 1, col - 1), (row - 1, col + 1), (row + 1, col - 1), (row + 1, col + 1)):
        if pos == 1:
            count += 1
    return count

The system answers to me that there are no neighbours near the chosen cell. Please explain me what's wrong and thank you for attention!

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
laVitaEBella
  • 25
  • 1
  • 3
  • 1
    I see two obvious mistakes: 1) you replaced the `grid` with an empty tuple and 2) your code doesn't reference the `grid` variable at all, you just add 1 to `count` if `pos` is equal to 1. `pos` will never be equal to 1, because you are setting it to one of a series of *tuples*. Ergo, your function will **always** return `0` as long as `row` and `col` are numeric (and raises an exception otherwise). – Martijn Pieters Nov 09 '14 at 17:01

1 Answers1

3

I see two obvious mistakes:

  1. you replaced the grid with an empty tuple

  2. your code doesn't reference the grid variable at all, you just add 1 to count if pos is equal to 1. pos will never be equal to 1, because you are setting it to one of a series of tuples.

Ergo, your function will always return 0 as long as row and col are numeric (and raises an exception otherwise).

You need to actually reference the grid that is passed in:

def count_neighbours(grid, row, col):
    count = 0
    for pos in (
            (row - 1, col), (row + 1, col), (row, col - 1),
            (row, col + 1), (row - 1, col - 1), (row - 1, col + 1),
            (row + 1, col - 1), (row + 1, col + 1)):
        if grid[pos[0]][pos[y]] == 1:
            count += 1
    return count

I'm assuming here that the grid is a list of lists representing rows and cells.

Next, you'll have to handle your positions going out of bounds; there are no neighbours to the top of the first row, for example:

def count_neighbours(grid, row, col):
    count = 0
    for x, y in (
            (row - 1, col), (row + 1, col), (row, col - 1),
            (row, col + 1), (row - 1, col - 1), (row - 1, col + 1),
            (row + 1, col - 1), (row + 1, col + 1)):
        if not (0 <= x < len(grid) and 0 <= y < len(grid[x])):
            # out of bounds
            continue
        if grid[x][y] == 1:
            count += 1
    return count
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • Thanks a lot, but it doesn't work. Maybe because of preconditions, I forgot about it. Precondition: 3 ≤ len(grid) ≤ 10 all(len(grid[0]) == len(row) for row in grid) – laVitaEBella Nov 09 '14 at 17:52
  • @laVitaEBella: Those preconditions are not a problem; you haven't defined *clearly* what input is given, and what output is expected; no test cases in your question. And *it doesn't work* isn't anything to go by either. How doesn't it work? For what inputs? What output was expected and what did you get instead? – Martijn Pieters Nov 09 '14 at 22:21
  • I'm so sorry for a very bad explanation of the problem. I've successfully overcome the tests where the tuples' sizes were 5x5 and have stucked with 3x3 tuple. One more for example: count_neighbours(((1, 1, 1), (1, 1, 1), (1, 1, 1),), 0, 2) == 3 (neighbours), Problem is: "IndexError: tuple index out of range" – laVitaEBella Nov 09 '14 at 23:22
  • @laVitaEBella I had an error in testing the upper bounds; corrected now. Can you try again? – Martijn Pieters Nov 09 '14 at 23:29
  • 1
    Thank you very much, mr. Pieters, now everything is OK. I'm very grateful to you. Probably, I must to learn harder because the task wasn't really difficult in the issue. Thank you for help! – laVitaEBella Nov 09 '14 at 23:47