Matrix Implementation In Pythonwith spiral increasing values
This generates a square matrix with spiral inward increasing values. How can I reduce this code? This generates the code matrix.
Input Number * Input Number
Input Number = 3
Output
1 2 3
8 9 4
7 6 5
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
Matrix Implementation In Python
How can I reduce this code? This generates the code matrix.
Input Number * Input Number
Input Number = 3
Output
1 2 3
8 9 4
7 6 5
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
Matrix with spiral increasing values
This generates a square matrix with spiral inward increasing values. How can I reduce this code?
Input Number = 3
Output
1 2 3
8 9 4
7 6 5
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
How can I reduce this code? This generates the code matrix.
Input Number * Input Number
Input Number = 3
Output
1 2 3
8 9 4
7 6 5
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
How can I reduce this code?
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
How can I reduce this code? This generates the code matrix.
Input Number * Input Number
Input Number = 3
Output
1 2 3
8 9 4
7 6 5
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")
Matrix Implementation In Python
How can I reduce this code?
import math
row=int(input("Input nomber := "))
lis=[[0 for i in range(0,row)]for j in range(0,row)]
y=row
a=1
c=0
z=0
x=1
k=1
f=row/2
f=math.ceil(f)
for b in range(0,f):
if b==k:
row=row-1
x=x+1
z=z+1
k=k+1
for c in range(z,row):
lis[b][c]=a
a=a+1
for d in range(x,row):
lis[d][row-1]=a
a=a+1
for e in range(row-1,z,-1):
lis[row-1][e-1]=a
a=a+1
for f in range(row-2,z,-1):
lis[f][z]=a
a=a+1
for i in range(0,y):
print()
for j in range(0,y):
print(lis[i][j],end="\t")