[英]How to make a chessboard pattern with a nested loop?
我需要制作一個看起來像這樣的模式:
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
0 1 0 1 0 1 0 1
到目前為止,我只能與那些對角線,但我需要一些幫助來理解如何做到這一點。
到目前為止我的代碼:
dimension = int(input('Enter dimension of board: '))
if dimension < 2:
print('Invalid input')
else:
for r in range(dimension):
for c in range(dimension):
print(int(c == r), end=' ')
print()
(c + r + 1) % 2
應該可以代替int(c==r)
。
這應該能讓你到達你想去的地方。
for r in range(dimension):
for c in range(dimension):
print(int((c + r + 1) % 2), end=' ')
print()
#List comprehension can be used here.
def print_board():
n=int(input('Enter dimension of board: '))
row=[[(x+1)%2 for x in range(n)], [x%2 for x in range(n)]]
board=[row[x%2] for x in range(n)]
for r in board:
for c in r: print(c, end=' ')
print(' ')
這是 balabhi 技術的一種變體。 它使用嵌套列表理解來構造偶數行和奇數行,然后使用str.join
方法將每一行的單元格連接成一個字符串。 make_board
函數返回一個行字符串列表,這些行字符串通過另一個.join
調用連接成一個單獨的字符串用於打印。
def make_board(side):
r = range(side)
rows = [' '.join(['01'[(i + p) % 2] for i in r]) for p in (1, 0)]
return [rows[i % 2] for i in r]
# Test
for i in range(0, 9):
board = make_board(i)
print('{0}:\n{1}\n'.format(i, '\n'.join(board)))
輸出
0:
1:
1
2:
1 0
0 1
3:
1 0 1
0 1 0
1 0 1
4:
1 0 1 0
0 1 0 1
1 0 1 0
0 1 0 1
5:
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
0 1 0 1 0
1 0 1 0 1
6:
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
1 0 1 0 1 0
0 1 0 1 0 1
7:
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
0 1 0 1 0 1 0
1 0 1 0 1 0 1
8:
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
0 1 0 1 0 1 0 1
我知道,現在可能有點晚了,但這是一個非常通用且同時快速的解決方案:
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'))
您還可以定義應該重復的立方體的大小。 在我的 PC 上創建 5000x5000 陣列大約需要 0.74 秒。
如果有什么不清楚的,請隨時提問。
這將為您提供所需的電路板輸出:
def printBoard(dimensions):
for row in range(dimensions**2):
if row % dimensions == 0 and row != 0:
print()
print((row + 1) % 2, end=' ')
else:
print((row + 1) % 2, end=' ')
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.