Reputation: 35
In input we have matrix with any count of rows and columns, for example:
1 2 3
4 5 6
7 8 9
In output should be:
1 4 7 8 9
0 2 5 6 0
0 0 3 0 0
I tried write code, but i had many errors, feel like I chose the wrong way and all of my researches wrong. How to solve this task?
I'm a student and my teacher gave me this task, it was unexpected, cause I training in codewars and forgot to learn matrix and numpy, i have 2 hours, can somedoby help me?
Upvotes: 1
Views: 139
Reputation: 25023
If your matrix is a square matrix, then
In [39]: a = [[1, 2, 3],
...: [4, 5, 6],
...: [7, 8, 9]]
...:
...: print(*([0 for j in range(i)] +
...: [a[j][i] for j in range(len(a)-i)] +
...: [a[len(a)-1-i][j] for j in range(i+1,len(a))] +
...: [0 for j in range(i)]
...: for i in range(len(a))), sep='\n')
[1, 4, 7, 8, 9]
[0, 2, 5, 6, 0]
[0, 0, 3, 0, 0]
In [40]:
does the trick
Upvotes: 0
Reputation: 4130
DAMN!! I am little bit late to party.Try this one.
a = (np.r_[t[0:rows-i,i],t[rows-1-i,i+1:]])
is the meat of the code.Which takes outer values of the matrix and concatenate in each iteration.next pad the array with 0s.
import numpy as np
t=np.array([[1, 2, 3],
[4 ,5 ,6],
[7, 8 ,9]])
result = []
rows,columns = t.shape
for i in range(rows):
a = np.r_[t[0:rows-i,i],t[rows-1-i,i+1:]]
pad = int(((rows+columns)-1-len(a))/2)
a = np.pad(a,(pad,pad), 'constant', constant_values=(0,0))
result.append(list(a))
print(result)
output
[[1, 4, 7, 8, 9], [0, 2, 5, 6, 0], [0, 0, 3, 0, 0]]
for input
t=np.array([[1, 2, 3,4],
[4 ,5 ,6,10]])
output
[[1, 4, 5, 6, 10], [0, 2, 3, 4, 0]]
Upvotes: 0
Reputation: 136
def rearrange(input_str):
src = [list(map(int, i.split())) for i in input_str.split('\n')]
mx, sumdata = len(src), []
for index, (ix, iy) in enumerate(zip(range(mx), range(mx - 1, -1, -1))):
data = ([0] * index) + list(range(src[0][ix], src[iy][ix], mx))
data.extend((*range(src[iy][ix], src[iy][mx - 1] + 1), *([0] * index)))
sumdata.append(data)
return sumdata
>>> print(data1)
1 2 3
4 5 6
7 8 9
>>> print(data2)
0 1 2 3
4 5 6 7
8 9 10 11
12 13 14 15
>>> pprint.pprint(rearrange(data1))
[[1, 4, 7, 8, 9], [0, 2, 5, 6, 0], [0, 0, 3, 0, 0]]
>>> pprint.pprint(rearrange(data2))
[[0, 4, 8, 12, 13, 14, 15],
[0, 1, 5, 9, 10, 11, 0],
[0, 0, 2, 6, 7, 0, 0],
[0, 0, 0, 3, 0, 0, 0]]
Get the turning point through the reverse cycle
Traverse vertical data, traverse horizontal data
Upvotes: 0
Reputation: 972
Try this:
l=[[1,2,3],[4,5,6],[7,8,9]]
r=[]
for j in range(len(l[0])):
r.append([])
r[j].extend([0 for i in range(j)])
r[j].extend([l[i][0] for i in range(0,len(l))])
r[j].extend([l[-1][i] for i in range(1,len(l))])
r[j].extend([0 for i in range(j)])
l=[[i for i in l[k][1:]] for k in range(len(l)-1)]
print(r)
Result
[[1,4,7,8,9],
[0,2,5,6,0],
[0,0,3,0,0]]
Upvotes: 0
Reputation: 2243
Complete Code
input_array=[[1,2,3],[4,5,6],[7,8,9]] #input array
row=len(input_array) #no of rows in array
col=len(input_array[0]) #no of columns in array
for i in range(0,col): #iterating for each head value
turn=row-i
for j in range(0,turn): #itearing and taking turn
if j==0:
for l in range(i): #for printing zeros in the beginning
print(0,end=" ")
if j<=row:
print(input_array[j][i],end=" ")
if j==turn-1: #taking turn
for k in range(i+1,row):
print(input_array[j][k],end=" ")
for l in range(i): #for printing zeros in the beginning
print(0,end=" ")
print("\n") #line break
OUTPUT
I've commented the code for your ease. If you have any question regarding the code, feel free to ask.
Upvotes: 0
Reputation: 7985
You need to get the outer values in each iteration.
Like, first you need to get the pink arrow values, then red, then the rest value.
First you need to create the matrix from 1 to 10.
A = np.arange(1, 10).reshape(3, 3)
Then, usng a recursion you will take the outer values, you can use counter (cnt
) for this:
import numpy as np
def get_mat(mat, cnt, col):
if cnt == mat.shape[0]:
return col
for i in range(cnt):
col.append(0)
for i in range(0, len(mat) - cnt):
m = mat[i]
col.append(m[cnt])
for r in m[cnt+1:]:
col.append(r)
for i in range(cnt):
col.append(0)
return get_mat(mat, cnt+1, col)
A = np.arange(1, 10).reshape(3, 3)
cols = []
res = get_mat(A, 0, cols)
res = np.reshape(res, (3, 5))
print(res)
Result:
[[1 4 7 8 9]
[0 2 5 6 0]
[0 0 3 0 0]]
Upvotes: 1