Introduction
A spiral pattern of numbers is an interesting exercise where numbers are printed in a spiral shape. This pattern starts from the top-left corner and moves inwards in a clockwise direction. The challenge is to control the direction of movement (right, down, left, and up) and ensure the numbers are placed correctly.
Problem Statement
Create a Python program that:
- Accepts the size of the spiral (an
n x n
grid). - Prints a spiral pattern of numbers.
Example:
- Input:
size = 4
- Output:
1 2 3 4 12 13 14 5 11 16 15 6 10 9 8 7
Solution Steps
- Input the Size of the Grid: The user specifies the size of the grid (
n x n
). - Initialize the Grid: Create an empty
n x n
grid to store the numbers. - Use Loops to Control Movement: Use loops and direction changes to place the numbers in the grid in a spiral pattern (right, down, left, up).
- Print the Grid: After filling the grid with numbers, print the grid in the spiral format.
Python Program
# Step 1: Input the size of the spiral (n x n)
n = int(input("Enter the size of the spiral (n x n): "))
# Step 2: Initialize the n x n grid with zeros
spiral = [[0] * n for _ in range(n)]
# Step 3: Initialize variables for movement direction and boundaries
num = 1
top, bottom, left, right = 0, n - 1, 0, n - 1
# Step 4: Fill the grid in a spiral pattern
while top <= bottom and left <= right:
# Move right across the top row
for i in range(left, right + 1):
spiral[top][i] = num
num += 1
top += 1
# Move down the right column
for i in range(top, bottom + 1):
spiral[i][right] = num
num += 1
right -= 1
# Move left across the bottom row
if top <= bottom:
for i in range(right, left - 1, -1):
spiral[bottom][i] = num
num += 1
bottom -= 1
# Move up the left column
if left <= right:
for i in range(bottom, top - 1, -1):
spiral[i][left] = num
num += 1
left += 1
# Step 5: Print the spiral pattern
for row in spiral:
print(' '.join(map(str, row)))
Explanation
Step 1: Input the Size of the Spiral
- The program starts by asking the user to input the size of the spiral (
n x n
).
Step 2: Initialize the Grid
- An
n x n
grid is created and initialized with zeros using list comprehension.
Step 3: Initialize Movement Variables
- Variables
top
,bottom
,left
, andright
define the boundaries of the current movement. num
starts at 1 and increments as the spiral is filled.
Step 4: Fill the Grid in a Spiral Pattern
- The loop continues until the boundaries meet or cross. The numbers are filled in a spiral order:
- Move right across the top row.
- Move down the right column.
- Move left across the bottom row.
- Move up the left column.
- After each movement, the boundaries (
top
,bottom
,left
,right
) are adjusted.
Step 5: Print the Spiral Pattern
- After filling the grid, the spiral pattern is printed row by row.
Output Example
For size = 4
, the output will be:
1 2 3 4
12 13 14 5
11 16 15 6
10 9 8 7
For size = 5
, the output will be:
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
Conclusion
This Python program prints a spiral pattern of numbers in an n x n
grid using loops and directional changes. The numbers are placed in a spiral order (right, down, left, and up) while adjusting the boundaries after each movement. This exercise is useful for practicing loop control, grid manipulation, and output formatting in Python.
Comments
Post a Comment
Leave Comment