8. ...and Seek!



i just want to know why for col u put len(board[0])-1 instead of just len(board)-1

Replace this line with your code. 
def random_col(board):
    return randint(0, len(board[0]) - 1)


This allows for the board do have uneven dimensions. Width and height are treated as independent of each other.


i still dont quite get it haha. But if i were to wrote len(board) - 1 for col, would there be any error?


Not as long as board dimensions are equal for both height (rows) and width (columns). A square board could use a simple function for both random variables. The exercise code allows for them to be different from one another.

The following is one such simple function...

from random import randint

def ship(n):
    return tuple([randint(x, n -1) for x in [0, 0]])

a, b = ship(5)
print "row: %d, column: %d" % (a, b)

This will always be dependent upon a square board.


def random_col(board):
    return randint(0, len(board[0]) - 1)

still do not understand the difference between len(board[0] - 1) and len(board - 1)

could we use len(board[0] - 1) for random_col and random_row in the same time? :sunny:


to me, len(board[0] - 1) would return -1 right?


heh, got this one, I think

there is board like [[0],[1],...]
in our case board[0] is [O O O O O]
so length here len(board[0]) is 5, but could be more than 5
we wanna find a random number between 0 to 4
- they assumed (IMHO wrongly) that you will play with rows/columns which start from 0 to 4, so you need to add - 1

another thing,
len(board - 1) - is for rows
len(board[0] - 1)- is for columns
but not really true, because our board is just list with extra lists inside
len(board - 1) - is for "rows" - but trully for outter list [ [x] [ ] [ ] [ ] [ ] ]
len(board[0] - 1)- is for "columns" - for inner lists -[x]


See my post above yours. The answer is yes, if the board has the same dimensions for rows and columns, otherwise, no. As you observed in your last post, we can isolate columns from rows by using a subscript to identify an internal list. Assuming all rows are the same length, any existing index will do.


This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.