title: Rotting orange problem - cannot get expected value
body: <p>So the problem is given a basket of <strong>n</strong> rows and <strong>m</strong> columns, find the minimum number of days required to rot all oranges, knowing that there is always at least 1 rotten orange in the basket, and their positions are given as input nested list <strong>positions</strong>.</p>
  <p>(EDIT: apologies, forgot to add the rotting rule, but basically adjacent oranges to a rotten orange should rot the next day e.g. if orange at [1,1] is rotten today, oranges at [0,1], [2,1], [1,0], [1,2] should rot the next day)</p>
  <p>Just cannot get the expected output from the input below, and I cannot see where my code went wrong. Really starting to feel like the expected output given is somehow problematic. Would be great to have some advices.</p>
  <p><strong>Input</strong>: n = 3, m = 7, positions = [[2,4]]</p>
  <p><strong>Expected output</strong>: 4</p>
  <p><strong>Actual output</strong>: 6</p>
  <pre><code>def rottingOrange(n,m,positions):
      #Idea: Find cells that have not rotten
      #If either cell next to it have rotten, it will also rot
      #If there is no more cells that have not rot, stops
      
      def isValid(i,j,positions):
          #Determine if an orange position is adjacent to a rotten orange
          if [i-1,j] in positions or [i+1,j] in positions or [i,j-1] in positions or [i,j+1] in positions:
              return True
          else:
              return False
  
      def rot(grid,positions):
          #Find all oranges that should rot for the current day
          temp = []
          for i,j in grid:
              if isValid(i,j,positions): 
                  temp = temp + [[i,j]]
          if len(temp) &lt; 1:
              return &quot;cannot rot&quot;
          else:
              grid_new = [x for x in grid if x not in temp]
              positions_new = positions + temp
              return grid_new, positions_new
  
      def rotting(grid, positions):
          #find the number of days taken to rot all oranges
          if len(grid) &lt;= 0 or rot(grid, positions) == &quot;cannot rot&quot;:
              return 0
          else:
              grid_new = rot(grid,positions)[0]
              positions_new = rot(grid,positions)[1]
              if len(grid_new) == len(grid):
                  return rotting(grid_new,positions_new)
              else:
                  return 1 + rotting(grid_new,positions_new)
  
      def grid_creation(n,m,positions):
          #create grid of size n x m, and remove all oranges that have rotten
          grid = []
          for i in range(n):
              for j in range(m):
                  grid.append([i,j])
          for x in positions:
              if x in grid:
                  grid.remove(x)
          return grid
  
      if __name__ == &quot;__main__&quot;:
          grid = grid_creation(n,m,positions)
          return rotting(grid,positions)
  </code></pre>