+ 1

Spiral Numbers in advance

First, I would like to say thanks to Mr. Swapnil Srivastava for his post of coding challenges of Spiral Number. This is the best challenge for us to enhance our coding skills. In this challenge, many coders solved the problem by using a 2D matrix to store numbers, then display the the matrix after computing and storing these numbers. Think about when you face to solve the challenge with a big size of spiral matrix, say about 10,000, it will take about a minute to compute and fifteen minutes to display the matrix if you using this algorithm. Additional, we will spend a lot of resources, especially RAM to compute and to store the numbers. So, my question is, how can we compute numbers and display it immediately? I mean, should we not use any iterators, but some variables, to resolve the problem? Thank you.

19th May 2018, 3:33 PM
Nezumi Neko
Nezumi Neko - avatar
9 Antworten
+ 4
Nezumi Neko there are no iterators in my code; only an array. I like your code. Haven't as yet figured out how it does it, but I will. You really shouldn't use tabs as your spacing falls apart. You can use this to fix it: print("%3d" % (b - j + i), end=" ")
25th May 2018, 10:44 AM
John Wells
John Wells - avatar
25th May 2018, 1:42 AM
John Wells
John Wells - avatar
+ 3
Updated to use your calculations: https://code.sololearn.com/clR1qmhXhW9K
27th May 2018, 11:36 AM
John Wells
John Wells - avatar
+ 2
Immediately, isn't possible. You could calculate a number at a time to display starting with what would have been stored in matrix[0,0] to matrix[0,99]. Coming up with the formula would be tricky, but proving it out on 5x5 and 6x6 should prove it for all n's. It is definitely possible. I did it for each line by figuring out that 21-11 (4 down, 4 left, plus 3 up) gave 10. My guess is knowing what row you're working on keeping an array of the last number in each column you can calculate any size matrix like I just did for this. 25 24 23 22 21 10 9 8 7 20 11 2 1 6 19 12 3 4 5 18 13 14 15 16 17
24th May 2018, 11:26 PM
John Wells
John Wells - avatar
+ 2
John Wells , well, array in python is a kind of iterator. Thank you for your advice to fix its showing.
25th May 2018, 11:08 AM
Nezumi Neko
Nezumi Neko - avatar
+ 1
Vote for your algorithm, John Wells, but, yours still uses iterators. I found a way to solve this problem using some formulas. We could calculate then print the value of any item by using a couple nest for loops. I will post my code soon.
25th May 2018, 6:52 AM
Nezumi Neko
Nezumi Neko - avatar
+ 1
25th May 2018, 7:07 AM
Nezumi Neko
Nezumi Neko - avatar
+ 1
Goodjob man, John Wells . Thanks for sharing 👍👍👍
27th May 2018, 11:44 AM
Nezumi Neko
Nezumi Neko - avatar
0
Dammmmn, you <swear> are awesome!!!! I can only hope to become as good a coder as you guys. #CodersShapeTheWorld
26th May 2018, 9:14 AM
Andrew Goodwin
Andrew Goodwin - avatar