目录
T1:国际象棋:
编辑
官方题解:(采用numpy的切片)
其他较好题解:
T1:国际象棋:
官方题解:(采用numpy的切片)
import numpy as np
def create_metrix() -> np.ndarray:
chessBoard = np.zeros((8, 8), dtype=int)
chessBoard[1::2, ::2] = 1
chessBoard[::2, 1::2] = 1
return chessBoard
import numpy as np
def create_metrix() -> np.ndarray:
chessBoard = np.zeros((8, 8), dtype=int)
chessBoard[1::2, ::2] = 1
chessBoard[::2, 1::2] = 1
return chessBoard
关键在于: 维度的理解(第0维表示x轴横向,第1维表示y轴纵向)
相关切片的讲解文章https://blog.csdn.net/weixin_36670529/article/details/111307004
其他较好题解:
import numpy as np
def create_metrix() -> np.ndarray:
metrix = np.zeros((8, 8), dtype=int)
for x in range(8):
for y in range(8):
if (x+y) % 2 != 0:
metrix[x][y] = 1
return metrix
T2:subplot绘图
官方题解:
# coding: utf-8
import matplotlib.pyplot as plt
def subplot_image(input_dir: str):
"""
:param input_dir: The direction of input image, like: '../input/img.png'
"""
# write your code here
img=plt.imread(input_dir)
for i in range(4):
k=i+1
plt.subplot(2,2,k)
plt.imshow(img)
其他解法:
# coding: utf-8
import matplotlib.pyplot as plt
def subplot_image(input_dir: str):
"""
:param input_dir: The direction of input image, like: '../input/img.png'
"""
# write your code here
data = plt.imread(input_dir)
# way-1
fig,axs = plt.subplots(2,2)
axs[0,0].imshow(data)
axs[0,1].imshow(data)
axs[1,0].imshow(data)
axs[1,1].imshow(data)
#way-2
# plt.subplot(2,2,1)
# plt.imshow(data)
# plt.subplot(2,2,2)
# plt.imshow(data)
# plt.subplot(2,2,3)
# plt.imshow(data)
# plt.subplot(2,2,4)
# plt.imshow(data)
# way-3
# fig = plt.figure()
# ax1 = fig.add_subplot(221)
# ax1.imshow(data)
# ax2 = fig.add_subplot(222)
# ax2.imshow(data)
# ax3 = fig.add_subplot(223)
# ax3.imshow(data)
# ax4 = fig.add_subplot(224)
# ax4.imshow(data)



