PY
py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# Spiral matrix
# Code
def pat(n,p,i,s,y=0,x=0,f="#"):
a,d=[[f]*n for k in range(n)],[0,1,0,-1]
while a[x][y]==f:
while min(x+1,y+1,n-x,n-y)>0 and a[x][y]==f:
a[x][y],x,y=(s[next(i):]+f)[0],x+d[p],y+d[1]
x,y,d=x-d[p],y-d[1],d[1:]+d[:1]
x,y=x+d[p],y+d[1]
return("\n".join(["".join(l) for l in a]))
# Test
s="There is a powerful force that science cant explain totally.This force is LOVE. "
n=int(len(s)**0.5)
print("Clockwise")
print("\n",pat(n,0,iter(range(n*n)),s))
print("\nCounter clockwise")
print("\n",pat(n,2,iter(range(n*n)),s,n-1))
Enter to Rename, Shift+Enter to Preview
OUTPUT
Run