Tôi biết, có lẽ hơi muộn một chút, nhưng đây là một giải pháp rất chung chung và đồng thời nhanh chóng:
1 import numpy as np
2 import matplotlib.pyplot as plt
3 import time
4 from PIL import Image
5 import os
6
7 def divmod_max_into[div_mod]:
8 if div_mod[1] > 0:
9 return div_mod[0] + 1
10 else:
11 return div_mod[0]
12 def create_chessboard[dim_x, dim_y, cube_size]:
13 start = time.time[]
14 light_grey = 230
15 dark_grey = 90
16 divmod_x_cube = divmod[dim_x, cube_size*2]
17 divmod_y_cube = divmod[dim_y, cube_size*2]
18 large_cube = np.full[[cube_size*2, cube_size*2], False]
19 large_cube[:cube_size, :cube_size] = True
20 large_output = np.tile[large_cube, [divmod_max_into[divmod_x_cube], divmod_max_into[divmod_y_cube]]]
21 large_output = np.transpose[large_output] + large_output == 2
23 output = np.full[large_output.shape, dark_grey, dtype=np.uint8]
24 output[large_output] = 230
25 print["Execution took {:.6f} seconds!".format[time.time[]-start]]
26 return output[:dim_x, :dim_y]
27
28 Image.fromarray[create_chessboard[5000, 5000, 3]].save[os.path.join[os.path.dirname[os.path.realpath[__file__]], 'out.tif']]
Bạn cũng có thể xác định kích thước của khối lập phương, nên được lặp lại. Tạo một mảng 5000x5000 mất khoảng 0,74 giây trên PC của tôi.
Nếu một cái gì đó không rõ ràng, hãy hỏi.
Cho n, in mẫu & nbsp; bảng kiểm tra cho ma trận n x n & nbsp; Mẫu bảng kiểm tra cho n = 8: Nó bao gồm các bình phương n * n xen kẽ 0 cho màu trắng và 1 cho màu đen. & Nbsp; Checkerboard Pattern for n = 8: It consists of n * n squares of alternating 0 for white and 1 for black.
Được đề xuất: Vui lòng thử cách tiếp cận của bạn trên {IDE} trước, trước khi chuyển sang giải pháp.{IDE} first, before moving on to the solution.
Chúng ta có thể làm tương tự bằng cách sử dụng lồng nhau cho các vòng lặp và một số điều kiện nếu điều kiện, nhưng sử dụng thư viện Numpy Python, chúng ta có thể nhập một ma trận 2 chiều và lấy mẫu bảng kiểm tra bằng cách cắt lát. W2, sẽ sử dụng chức năng Python sau để in mẫu:
x = np.zeros[[n, n], dtype=int]
Sử dụng chức năng này, chúng tôi khởi tạo ma trận 2 chiều với 0 Lừa ở tất cả chỉ mục bằng cách sử dụng Numpy
- X [1 :: 2, :: 2] = 1: Cắt lát từ hàng chỉ số 1 đến 1+2+2 và lấp đầy tất cả các cột với 1 bắt đầu từ 0 đến 0+2+2, v.v.: Slice from 1st index row till 1+2+2… and fill all columns with 1 starting from 0th to 0+2+2… and so on.
- X [:: 2, 1 :: 2] = 1: Cắt lát từ hàng 0 đến 0+2+2 và lấp đầy tất cả các cột với 1 bắt đầu từ 1 đến 1+2+2+: Slice from 0th row till 0+2+2… and fill all columns with 1 starting from 1 to 1+2+2+…..
Chức năng của np.zeros [[n, n], dtype = int]: Thông thường, các phần tử của một mảng ban đầu chưa được biết, nhưng kích thước của nó được biết đến. Do đó, Numpy cung cấp một số chức năng để tạo các mảng có nội dung giữ chỗ ban đầu. Những điều này giảm thiểu sự cần thiết của các mảng phát triển, một hoạt động đắt tiền. Sử dụng tham số DTYPE khởi tạo tất cả các giá trị với loại dữ liệu INT. Ví dụ: np.zeros, np.ones, vv & nbsp;Often, the elements of an array are originally unknown, but its size is known. Hence, NumPy offers several functions to create arrays with initial placeholder content. These minimize the necessity of growing arrays, an expensive operation. Using the dtype parameter initializes all the values with int data-type. For example: np.zeros, np.ones etc.
Python3
import
numpy as np
def
printcheckboard[n]:
print
x = np.zeros[[n, n], dtype=int]0
def
7
x = np.zeros[[n, n], dtype=int]3
def
9
Checkerboard pattern: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]9
import
0import
1 import
2import
3Các
Checkerboard pattern: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]9
import
0import
1 import
2import
3Các
numpy as np
0print
numpy as np
2
x = np.zeros[[n, n], dtype=int]3
numpy as np
4import
4print
numpy as np
7
numpy as np
8
x = np.zeros[[n, n], dtype=int]3
def
0def
1
Output:
Checkerboard pattern: 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0 0 1 0 1 0 1 0 1 1 0 1 0 1 0 1 0
Mã nguồn được cải thiện dựa trên giả định rằng bảng kiểm tra luôn là một NXN, tức là, n chẵn & nbsp;
Python3
import
numpy as np
def
printcheckboard[n]:
def
7
x = np.zeros[[n, n], dtype=int]3
def
9
Checkerboard pattern: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]9
import
0import
1 import
2import
3Các
print
x = np.zeros[[n, n], dtype=int]10
numpy as np
8
x = np.zeros[[n, n], dtype=int]3
def
0def
1
Output:
Checkerboard pattern: [[0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0] [0 1 0 1 0 1 0 1] [1 0 1 0 1 0 1 0]]