|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 欺Ran 于 2021-11-19 10:15 编辑
一个矩阵内给定两个坐标,截取两个坐标点内可以形成的矩形
请用loop完成
不能用到break/continue.
例子:
[[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
给定坐标(1, 1), (2, 2)
返回[[6, 7], [10, 11]]
给定坐标(0, 0), (3, 0)
返回[[1], [5], [9], [13]]
给定坐标(0, 3), (0, 0)
返回[[1, 2, 3, 4]]
def test(cd1, cd2):
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
result = []
rmi = min(cd1[0], cd2[0])
rma = max(cd1[0], cd2[0])
cmi = min(cd1[1], cd2[1])
cma = max(cd1[1], cd2[1])
r = rmi
while r <= rma:
result.append(matrix[r][cmi:cma + 1])
r += 1
print(result)
test([1, 1], [2, 2])
test([0, 0], [3, 0])
test([0, 3], [0, 0])
def test(cd1, cd2):
matrix = [[1, 2, 3, 4],
[5, 6, 7, 8],
[9, 10, 11, 12],
[13, 14, 15, 16]]
result = []
rmi = min(cd1[0], cd2[0])
rma = max(cd1[0], cd2[0])
cmi = min(cd1[1], cd2[1])
cma = max(cd1[1], cd2[1])
for m in matrix[rmi:rma + 1]:
result.append(m[cmi:cma + 1])
print(result)
test([1, 1], [2, 2])
test([0, 0], [3, 0])
test([0, 3], [0, 0])
|
|