基于坐标系生成值
Producing a value based off a coordinate system
我遇到了一个必须解决的问题
build_board(coords,size):
Given a list of coordinates for the locations of
int size, build a board (a list of lists of Booleans) of that size, and mark cells with a queen
True.
示例:
build_board([(0,0)],1) → [[True]]
build_board([(0,0)],2) → [[True, False], [False, False]]
build_board([(0,0),(1,1)],2) → [[True, False], [False, True]]
上下文是我们做了一个函数来制作板子
def build_empty_board(size):
size=int(size)
ans = [ [False for x in range(size)] for x in range(size) ]
return ans
但是我不知道如何编写一个循环来检查每个制作的板并从坐标系中产生值。谁能指导我如何编写代码?
这种方法怎么样:
def build_board(coords, size):
# if any(i < 0 for coord in coords for i in coord):
# return
board = [[False] * size for _ in range(size)]
for (row, col) in coords:
if row < 0 or col < 0:
return
board[row][col] = True
return board
print(build_board([(0,0)],1)) #[[True]]
print(build_board([(0,0)],2)) #[[True, False], [False, False]]
print(build_board([(0,0),(1,1)],2)) #[[True, False], [False, True]]
print(build_board([(0,0),(-1,3)],2)) #None
我遇到了一个必须解决的问题
build_board(coords,size):
Given a list of coordinates for the locations of
int size, build a board (a list of lists of Booleans) of that size, and mark cells with a queen
True.
示例:
build_board([(0,0)],1) → [[True]]
build_board([(0,0)],2) → [[True, False], [False, False]]
build_board([(0,0),(1,1)],2) → [[True, False], [False, True]]
上下文是我们做了一个函数来制作板子
def build_empty_board(size):
size=int(size)
ans = [ [False for x in range(size)] for x in range(size) ]
return ans
但是我不知道如何编写一个循环来检查每个制作的板并从坐标系中产生值。谁能指导我如何编写代码?
这种方法怎么样:
def build_board(coords, size):
# if any(i < 0 for coord in coords for i in coord):
# return
board = [[False] * size for _ in range(size)]
for (row, col) in coords:
if row < 0 or col < 0:
return
board[row][col] = True
return board
print(build_board([(0,0)],1)) #[[True]]
print(build_board([(0,0)],2)) #[[True, False], [False, False]]
print(build_board([(0,0),(1,1)],2)) #[[True, False], [False, True]]
print(build_board([(0,0),(-1,3)],2)) #None