0

I want to print all the neigbours of a 2 dimensional array. For that I found this pseudocode but have problems translating it into python.

Can someone explain what does max() and min() are supposed to do? I think it's a range but not sure

row_limit = count(array);
if(row_limit > 0){
    column_limit = count(array[0]);
    for(x = max(0, i-1); x <= min(i+1, row_limit); x++){
      for(y = max(0, j-1); y <= min(j+1, column_limit); y++){
        if(x != i || y != j){
          print array[x][y];
      }
    }
  }
}

This is what I have so far:

def surrounded(board, row, col): 
    row_limit = len(board);
    if row_limit > 0:
        column_limit = len(board[0]);
        for x in range(0, row-1):
            if x <= min(row+1, row_limit):
                for y in range(0, col-1):
                    if x <= min(j+1, column_limit):
                        if x != row and y != col:
                            print board[row][col];
Juanvulcano
  • 1,354
  • 3
  • 26
  • 44
  • 1
    equivalent of `for(x=A; x<=B; x++)` in python is `for x in range(A, B+1):` just literaly apply it to your pseudocode and you are done. `min` and `max` are well defined in python – lejlot Dec 08 '15 at 14:50
  • 1
    `max(a,b)` means whichever of `a` or `b` is larger. Python has `min` and `max` functions builtin. – interjay Dec 08 '15 at 14:51
  • Thanks :) Now I can translate it – Juanvulcano Dec 08 '15 at 14:51
  • Where do `i` and `j` come from in the pseudocode? Maybe explain that you're trying to do without the pseudo code? What are the neighbours you want to print? – Maciek Dec 08 '15 at 14:51
  • @Maciek I grabbed the code as it is but i and j are I for row number and J for column number – Juanvulcano Dec 08 '15 at 14:52

0 Answers0