8. ...and Seek!


#1



https://www.codecademy.com/courses/python-beginner-en-4XuFm/0/8?curriculum_id=4f89dab3d788890003000096


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)


#2

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


#3

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


#4

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.


#5

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:


#6

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


#7

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
so,
len(board - 1) - is for "rows" - but trully for outter list [ [x] [ ] [ ] [ ] [ ] ]
len(board[0] - 1)- is for "columns" - for inner lists -[x]


#8

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.


#9

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