Minimum number of cells after, before, above and below NxN squares: Difference between revisions

m
→‎{{header|Python}}: : arbitrary matrix size printing
m (→‎{{header|Perl}}: adapt for multiple input N)
m (→‎{{header|Python}}: : arbitrary matrix size printing)
Line 1,175:
 
=={{header|Python}}==
<lang python>def printMinCellsmin_cells_matrix(siz):
{{trans|Wren}}
return [[min(row, col, siz - row - 1, siz - col - 1) for col in range(siz)] for row in range(siz)]
<lang python>def printMinCells(siz):
 
def display_matrix(mat):
siz = len(mat)
spaces = 2 if siz < 20 else 3 if siz < 200 else 4
print(f"\nMinimum number of cells after, before, above and below {siz} x {siz} square:")
for row in range(siz):
cells = print("".join([min(row, col, siz - row - 1, siz - col - 1)f"{n:{spaces}}" for coln in range(sizmat[row]]))]
 
print(" ".join([str(n) for n in cells]))
def test_min_mat():
for gridsizesiz in [23, 10, 9, 2, 1]:
display_matrix(min_cells_matrix(siz))
printMinCells(gridsize)
 
if __name__ == "__main__":
test_min_mat()
</lang>{{out}}
<pre>
Same as Wren version.
Minimum number of cells after, before, above and below 23 x 23 square:
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 11 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 10 10 10 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 9 9 9 9 9 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 8 8 8 8 8 8 8 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 7 7 7 7 7 7 7 7 7 6 5 4 3 2 1 0
0 1 2 3 4 5 6 6 6 6 6 6 6 6 6 6 6 5 4 3 2 1 0
0 1 2 3 4 5 5 5 5 5 5 5 5 5 5 5 5 5 4 3 2 1 0
0 1 2 3 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 3 2 1 0
0 1 2 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 10 x 10 square:
0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 4 4 3 2 1 0
0 1 2 3 3 3 3 2 1 0
0 1 2 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 9 x 9 square:
0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 0
0 1 2 2 2 2 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 3 4 3 2 1 0
0 1 2 3 3 3 2 1 0
0 1 2 2 2 2 2 1 0
0 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0
 
Minimum number of cells after, before, above and below 2 x 2 square:
0 0
0 0
 
Minimum number of cells after, before, above and below 1 x 1 square:
0
</pre>
 
Or, disentangling computation from IO (separating model from display), and composing from generics:
4,105

edits