mama
mama

Reputation: 2227

How can I make a square with a specified circumference and add margin?

I am trying to make a square path of a specified length:

I made a function - and if I put 20 then I get a 6x6 matrix.

How can I add a margin of 0's of eg. 3 fields thickness?

like this

0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 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 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 0 0 0
0 0 0 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 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0
def square(length): return [
    [1 for _ in range(length//4+1)]
    for _ in range(length//4+1)
]

for x in square(24):
    print(x)

Upvotes: 0

Views: 37

Answers (3)

Alain T.
Alain T.

Reputation: 42143

You can prepare a line pattern of 0s and 1s then build a 2D matrix by intersecting them.

def square(size,margin=3):
    p = [0]*margin + [1]*(size-2*margin) + [0]*margin
    return [[r*c for r in p] for c in p]

for row in square(20):print(*row)

0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 0 0 0
0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0

Upvotes: 1

match
match

Reputation: 11060

One way to do this is to build it as a flat string, then use textwrap to style the output into the right number of lines:

import textwrap

# The number of 1's in a row/column
count = 6
# The number of 0's to pad with
margin = 3
# The total 'size' of a row/column
size = margin + count + margin

pad_rows = "0" * size * margin
core = (("0" * margin) + ("1" * count) + ("0" * margin)) * count
print('\n'.join(textwrap.wrap(pad_rows + core + pad_rows, size)))

Upvotes: 0

Tim Roberts
Tim Roberts

Reputation: 54718

Here's one way. One caution here is that, because of the way I duplicated the zero rows, those are all the same list. If you modify one of the zero rows, it will modify all of them.

def square(length): 
    zeros = [0]*(length//4+7)
    sq = [zeros] * 3
    sq.extend( [
        ([0,0,0] + [1 for _ in range(length//4+1)] + [0,0,0] )
        for _ in range(length//4+1)
    ])
    sq.extend( [zeros]*3 )
    return sq

for x in square(24):
    print(x)

Here's a numpy method.

import numpy as np
def square(length): 
    c = length//4+1
    sq = np.zeros((c+6,c+6)).astype(int)
    sq[3:c+3,3:c+3] = np.ones((c,c))
    return sq

print( square(24) )

Upvotes: 1

Related Questions