- 1
How can we traverse a list spirally?
Eg: input -> 9 0 1 2 3 4 5 6 7 8 Matrix should be : 0 1 2 7 8 3 6 5 4 messed up by using some directional flags concept,but im stuck! Ps: input will be for square matrix!
17 Answers
+ 1
RuntimeERROR
Can u do this with your direction way?
l=left, d=down, r=right,u= up
1l 1d
2r 2u .
3l 3d
4r 4u.....
876
105
234
Here it is for input 9..
Max=8
Now for each number n in spiral
max- n
0
Abhay input is a list of values, and output must be the square matrix filled with values in spiral ;)
0
RuntimeERROR however, you could use list comprehension to initialize the matrix:
n = int(input(n))
s = int(sqrt(n))
m = [[None for c in range(s)] for r in range(s)]
- 1
sounds like some codewars kata(s)... so we are not allowed to help you (neither by sololearn policy than by codewars policy ^^)...
however, I could give some hint:
you should use square root of list length to find both equals dimension of matrix (9 => 3) and search for an algorithm to walk inside 2d list (matrix) spiraly, so that for 3×3 matrix you fill cells in this order:
[0][0]
[0][1]
[0][2]
[1][2]
[2][2]
[2][1]
[2][0]
[1][0]
[1][1]
... but your algorithm must work for any n×n matrix: that's quite tricky, but reachable ^^
- 1
Abhay solve it if you want, but don't give answer: it's against sololearn rules and codewars rules ;P
- 1
list comprehension is not the easiest way to achieve that, maybe not possible at all ;)
- 1
RuntimeERROR yes, that's for construction of list of values wich should be filled in the array... but firstly that's not list comprehension, and secondly you are not required to construct that list ^^
instead you have to input the total length of values n=int(input()), create a 2d list of sqrt(n) in both dimension filled with anything, and then find a way to iterate over it in spiral to put 0 to n-1 numbers (using another variable initialized to 0 and incremented each time you fill a cell ;))
- 1
start from the top left corner of the matrix and set up 4 states for the loop.
left -> down -> right -> up.
state would change in the following order if it reaches end of matrix or a number is reached.
if the first number it checks is a number then the matrix is filled already.
- 1
Import numpy as np
n=int (input ())
a=[]
for i in range (n):
a.append (input ())
b=np.array (a)
k=b.reshape (r,m) #m×r=n <--it is important
print (k)
- 2
Output will be a square matrix or input will be a square matrix ?
otherwise what will be the output?
- 2
Abhay we have to construct a list,from input,which is suited for square matrix like 1,4,9,16.. and
Matrix which is list of lists is required
- 2
No visph only number by using list comprehension we construct the list, and then i observed is that for example given is 8 so first row is 3 elements in right direction,next 2 down,2 left,1 up,1 right so a total of 2*sqrt(n+1)-1 times going through a loop, correct me if im wrong
- 2
RuntimeERROR what do you mean ? But anyway i assume what you want is what visph answered to me .
- 2
visph im not into any challenge btw, im preparing for my coding interview
- 2
visph u got me wrong,how will u construct list if u have a number?
Like l=list(range(int(input()))
This is what im saying and problem is to construct matrix from this!