SPIRAL OF NUMBERS
Right now, I am in middle of my college stuff and that’s why the blogs are coming a little late, sorry for that. We just finished our BIG PROJECT on the last day of the last year which consisted of web development along with concepts of database management. I have posted a video regarding the output of that project, if you want, you can visit the following link, https://www.facebook.com/ashishsharma260696/videos/210216116217633/ . It also has links regarding the blogs for the project and if you haven’t liked my page, Unoffical: Ashish Sharma, please like the page and feel free to post a review after going through my page. Before starting anything new, I thought of solving this problem regarding spiral of numbers.
So basically our program would get an integer as an input and then the numbers would go on to make a spiral. For example, for n=5, our output should look like,
1 2 3 4 5
16 17 18 19 6
15 24 25 20 7
14 23 22 21 8
13 12 11 10 9
As in C/C++ we have multi dimensional arrays, the concept of array does not exists in python. Instead of that, we have lists which can be nested. So we will be creating a nested list which would act as a 2D array. We would be initializing all the elements of our lists as zero at first so that a nested list is formed. We can do that as follows:
Now after this if we give the input as spiral(5), we can get a list with 5 nested lists each having 5 elements as shown below,
[[0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0]]
If we know how to do the program in C/C++, then from here onward we have to use the same approach. We can notice that the number are traversed in form of rings, first the outermost, then the inner one and so on. The number of such traversals depends upon the number, it can be found out using following,
import math
z=math.ceil(n/2)
Ceil() gives the next value to a floating number. Suppose n/2 gives us 4.5, then the z value would be set to 5. It basically takes the upper integer value. This gives us the number of traversals. Now in each traversal we have to fill our list in 4 parts as top, bottom, left and right. Let us consider our base case for n=5. For the top layer, initially loop should run 5 times, for i=0 to 4. Then for the next time for i=1 to 3 and then for i=2 to 2. So, our loop basically runs from i to n-i-1. Since it is the top layer, i value would remain same and j would increase each time. The count value is initialized as 1, hence our loop would be:
for i in range(z): # outer loop for number of rounds
for j in range(i,n-i): # top layer
li[i][j]=count;
count+=1;
Now the right layer should run for 4 times and i should start from the next value as now we have to start our descend. So we can use the same loop with j=i+1 and now our j value will remain the same but i would change,
Our bottom layer will start from n-i-1th element and it would go upto our ith element, this should be in decreasing order. It would also work for 4 times, which can be made to work as follows:
The left layer loop would work for 3 times, which is one time lesser than the bottom and right layer and 2 times lesser than the top layer. So it can be done in the same manner as bottom layer as a decrementing loop as follows,
Hence we can make a general idea that if the top layer loop is executing for n times then,
After this we can just print our nested list as follows,
You can check the full code at this link: https://drive.google.com/open?id=108dWFAlLH_mo5F0AviglI_2otiWwHr7k. Below is the output for our code, there may be more efficient and easy way to do this, but I found this way to be more transparent. People even used recursive approach for this problem, so whatever helps you, follow that, you can check the output:
I will try to start something new, if not I would share some short but interesting problems like this, stay happy and keep coding…
Thank you for reading, if you liked the article please click on the clap button and show your support. You can clap more than once, just keep pressing the icon!!!