title: How to get next and previous value in a 2D array (row major order) in Python?
body: <p>I'm working on two functions which take in a 2d array and a current row and column value. It should return the next value or the previous value (if there is one) and if there isn't it should return (-1,-1)</p>
  
  <p>In the functions, I call the value (number) the node and nodeSolution is the 2d array.</p>
  
  <p>My problem is that I keep getting errors in my use function when I use this to backtrack. I have been bug fixing but I cannot seem to find where my code is going wrong. I'm getting a lot of index out of range, etc.</p>
  
  <pre><code>def nextNode(row, col, nodeSolution):
      if(row == len(nodeSolution) and col == len(nodeSolution)):
          return (-1,-1)
      if(col == len(nodeSolution) - 1):
          return (row+1, 0)
      return (row, col+1)
  
  def previousNode(row, col, nodeSolution):
      if(row == 0 and col == 0):
          return (-1,-1)
      if(col == 0):
          return (row-1, len(nodeSolution) - 1)
      return (row, col-1)
  </code></pre>
  
  <p>Both functions should always return a row, col value that is in the 2d array or (-1,-1)</p>