K
K
kjjokay2021-03-07 08:46:42
Python
kjjokay, 2021-03-07 08:46:42

How to make spiral arrays?

Hi all! I'm just learning about arrays and Python in general. I recently saw such a thing as spiral two-dimensional arrays. I understand how to create them, but not completely. here's an example of how to create a spiral array starting from the bottom, or from the inside, etc. help me understand

Answer the question

In order to leave comments, you need to log in

1 answer(s)
W
Wataru, 2021-03-07
@wataru

Take a piece of paper, draw on it the spiral array you need. Look at her carefully.
You write out straight pieces - how long they are, in which direction they rotate. Look for a pattern (for example, in a spiral array from the center of the length of the pieces: 1, 1, 2, 2, 3, 3, ...).
Implement it. The outer loop runs until all numbers are set. Inside, recalculate the length and direction of the straight piece according to the pattern and start the cycle from the current position along the direction - put the numbers the required number of times.
To implement rotation, store the current direction as two variables dx, dy. One of them is +-1, and the other is 0. To rotate counterclockwise, do(dx, dy) = (-dy, dx)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question