- 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!

29th Jun 2021, 10:28 AM
RuntimeERROR
RuntimeERROR - avatar
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
1st Jul 2021, 6:53 AM
Oma Falk
Oma Falk - avatar
0
Abhay input is a list of values, and output must be the square matrix filled with values in spiral ;)
29th Jun 2021, 10:40 AM
visph
visph - avatar
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)]
29th Jun 2021, 11:02 AM
visph
visph - avatar
- 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 ^^
29th Jun 2021, 10:39 AM
visph
visph - avatar
- 1
Abhay solve it if you want, but don't give answer: it's against sololearn rules and codewars rules ;P
29th Jun 2021, 10:43 AM
visph
visph - avatar
- 1
list comprehension is not the easiest way to achieve that, maybe not possible at all ;)
29th Jun 2021, 10:46 AM
visph
visph - avatar
- 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 ;))
29th Jun 2021, 10:56 AM
visph
visph - avatar
- 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.
29th Jun 2021, 1:43 PM
Shen Bapiro
Shen Bapiro - avatar
- 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)
30th Jun 2021, 9:46 PM
Begench Gurbanmammedov
- 2
Output will be a square matrix or input will be a square matrix ? otherwise what will be the output?
29th Jun 2021, 10:39 AM
Abhay
Abhay - avatar
- 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
29th Jun 2021, 10:41 AM
RuntimeERROR
RuntimeERROR - avatar
- 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
29th Jun 2021, 10:44 AM
RuntimeERROR
RuntimeERROR - avatar
- 2
RuntimeERROR what do you mean ? But anyway i assume what you want is what visph answered to me .
29th Jun 2021, 10:45 AM
Abhay
Abhay - avatar
- 2
visph im not into any challenge btw, im preparing for my coding interview
29th Jun 2021, 10:46 AM
RuntimeERROR
RuntimeERROR - avatar
- 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!
29th Jun 2021, 10:48 AM
RuntimeERROR
RuntimeERROR - avatar