🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
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 ngrid). - 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 ngrid 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 ngrid is created and initialized with zeros using list comprehension.
Step 3: Initialize Movement Variables
- Variables
top,bottom,left, andrightdefine the boundaries of the current movement. numstarts 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.
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment