Spiral (Python) | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Spiral (Python)

The task of Python. Given an integer N. You need to create an array of size 2*N + 1. In the center of the array is the number 1. Example(if N = 2): 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 You must write the code by which the array will be filled counterclockwise, starting with 1. The result should be of this type (the order of filling should be identical to the example): 13 12 11 10 25 14 3 2 9 24 15 4 1 8 23 16 5 6 7 22 17 18 19 20 21 How to implement it without "import" (numpy, math etc.)? Interested in the implementation of only using loops(for).

12th Jun 2019, 11:47 AM
Dmitry Venzik
Dmitry Venzik - avatar
11 Answers
+ 3
There's always a pattern. It repeats every two numbers (N). We start with N = 2. [[3, 2] ,[4, 1]] The next N+1 terms are added at the end of the array. [[3, 2], [4, 1], [5, 6, 7]] All remaiming untouched lists inside the array are filled with the next N numbers starting with the right-most untouched list. The numbers are added at the end of each list. [[3, 2, 9], [4, 1, 8], [5, 6, 7]] We increase N by 1 (N = 3). The next N+1 terms are added in reverse order at the beginning of the array. [[13, 12, 11, 10], [3, 2, 9], [4, 1, 8], [5, 6, 7]] All remaining untouched list inside the array are filled with the next N number starting with the left-most untouched list. The numbers are added at the beginning of each list. [[13, 12, 11, 10], [14, 3, 2, 9], [15, 4, 1, 8], [16, 5, 6, 7]] We increase N by 1 and repeat the pattern (every two increases).
12th Jun 2019, 12:32 PM
Diego
Diego - avatar
+ 2
What have you tried so far? Take a look at a similar challenge. https://www.sololearn.com/learn/4543/
12th Jun 2019, 12:00 PM
Diego
Diego - avatar
+ 2
There are three actions: Finish if the target in front of you is out of board. Turn if the cell left hand side is zero. Otherwise move and increment value set. All you need is a 2d list, a key for moving ULDR, a row column position and a counter.
12th Jun 2019, 3:50 PM
tamaslud
tamaslud - avatar
+ 2
Dmitry Venzik that was just an outline. You may have the joy to write it in this way or another. I have a similar code with a bit more tricky rules for moves. https://code.sololearn.com/cuD4yIJFV1vI/?ref=app
12th Jun 2019, 8:28 PM
tamaslud
tamaslud - avatar
+ 2
https://code.sololearn.com/co8PFBt54mIp/?ref=app
13th Jun 2019, 3:35 PM
tamaslud
tamaslud - avatar
+ 1
Diego Wow, that's a really cool explanation. Thank you very much! Now I can write code and test it.
12th Jun 2019, 1:03 PM
Dmitry Venzik
Dmitry Venzik - avatar
+ 1
tamaslud Thanks! :) I'll try to understand your code while someone else's code is hard for me to understand. I want to understand the principle of operation of the filling of the array. I will run the code in the "debug" mode in PyCharm, it helps to understand how this or that code element works :)
13th Jun 2019, 3:48 PM
Dmitry Venzik
Dmitry Venzik - avatar
+ 1
tamaslud To be honest, I haven't tried to write my own code for spiral yet. Until they solve different tasks codeforces.com.
13th Jun 2019, 3:50 PM
Dmitry Venzik
Dmitry Venzik - avatar
+ 1
Dmitry Venzik i have also lost sometimes in rows and cols, but after some modifications it works. I have defined a tuple with moves up, down, l, r, udlrudlrudlr etc. and started to move from the middle. If the next type of turning offers un untouched matrix element aka zero it turns else going forward til i run out pf possible elements.
13th Jun 2019, 4:34 PM
tamaslud
tamaslud - avatar
0
"What have you tried so far?" Diego, What do you mean? I write code in PyCharm, tried to write a solution, but I don't quite understand how the universal code should look like, so that at any N the filling is the same. I want to understand in what direction to think.
12th Jun 2019, 12:07 PM
Dmitry Venzik
Dmitry Venzik - avatar
0
tamaslud Thanks for reply! Where can I see the implementation of this method in more detail?
12th Jun 2019, 5:40 PM
Dmitry Venzik
Dmitry Venzik - avatar