-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathpretty_square.py
More file actions
48 lines (39 loc) · 960 Bytes
/
pretty_square.py
File metadata and controls
48 lines (39 loc) · 960 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# Given a positive integer N, print pretty square of N.
# Pretty square of 1 is :
# 1
# pretty square of 2 is:
# 2 2 2
# 2 1 2
# 2 2 2
# pretty square of 3 is:
# 3 3 3 3 3
# 3 2 2 2 3
# 3 2 1 2 3
# 3 2 2 2 3
# 3 3 3 3 3
# Invokation: python3 pretty_square.py 5
import sys
N = sys.argv[1]
def printmat(n: int) -> None:
'''
Function takes integer input n and prints pretty matrix.
'''
pretty_mat = []
for i in range(2*n-1):
pretty_mat.append([0]*(2*n-1))
for i in range(2*n-1):
for j in range(2*n-1):
dis_i = min(i-0, (2*n-2)-i)
dis_j = min(j-0, (2*n-2)-j)
pretty_mat[i][j] = str(n-min(dis_i, dis_j))
print(f'Pretty Square of {n} is:')
for row in pretty_mat:
print(" ".join(row))
try:
N = int(N)
if(N > 0):
printmat(N)
else:
print("Please enter a positive integer greater than 0!")
except:
print("Please enter a valid integer!")