Sum neighbors of a given index in a bi dimensional matrix in Swift
I was solving this problem a couple days ago and I decided to post it here, so maybe I can help someone facing something similar.
Basically given a two dimensions matrix of numbers, we have to sum the value of the neighbors of an also given index. The index will be constituted of a line and column. Let's see an example of matrix that could be used in our algorithm.
This matrix shown is a possible example. It contains 10 lines and 4 columns. In order to test the algorithm I create a simple function to fill a empty matrix with random numbers, as is possible to see below:
Now that we have the matrix ready to be used by our algorithm, we can actually work on it. The first thing to analyse would be who's the neighbors of a given index, let's say index = (line: 5, column: 3). Analysing the figure posted before, or just using our imagination will notice that the neighbors are:
line 4, column 2
line 4, column 3
line 4, column 4
line 5, column 2
line 5, column 4
line 6, column 2
line 6, column 3
line 6, column 4
After a while it becomes obvious that the range of these neighbors is something similar to:
line = givenLine-1 ... givenLine+1
column = givenColumn-1 ... givenColumn+1
We need only remove the index given index, after all, the given index refers to the element itself, and not to one of its neighbors.
Also, something that is important to notice maybe is the way the matrix is filled. Basically the matrix is a list of list. I wrote below more or less how it can be imagined: It's a list of lines, where each line contains the number correspondent to a column. This information is important for us to understand the indexing of this list. So whenever we want to fetch the element on the position line y, column x we basically have to do matrix[y][x].
matrix = [
line 1 = [0,1,2,3...]
line 2 = [0,1,2,3...]
Before we start implementing, I'd like to discuss a little about the edge cases of this implementation. I mean, we have the right algorithm, but we still have to treat the edge cases that could possibly come to us. The first one I can think is, what if the given index is negative or bigger than the list size? Let's say, for that old example of matrix we gave in the beginning of this post, what if the user asks the sum for the element index line = 100, column 90? Or even line 2 and column -1? Let's treat this problem with a condition in the first lines of our function!
Another edge case is whenever the ranges of neighbors we found is bigger than the list or negative. We can solve this problem butting some restrictions in what is a valid range. For example:
For the line, the minimum range is going to be 0, and the maximum is going to be the size of the matrix-1
For the column the minimum still being 0 too, but the maximum now will be related to the given line. It will be the size of the number of columns in that line-1.
Let's code these conditions now.
Cool, we have our implementation working! Now we can find the sum of the elements around the element at the given index! I hope I have helped somehow. Leave a comment and let's discuss if there's any other way to implement this solution in a better computational time.