Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python program to print rangoli pattern using alphabets
A rangoli pattern is a diamond-shaped pattern that uses alphabets arranged symmetrically. Given a number n, we create an alphabet rangoli of size n where alphabets start from 'a' and go up to the nth letter of the alphabet.
Understanding the Pattern
For n = 5, the rangoli pattern looks like this:
--------e-------- ------e-d-e------ ----e-d-c-d-e---- --e-d-c-b-c-d-e-- e-d-c-b-a-b-c-d-e --e-d-c-b-c-d-e-- ----e-d-c-d-e---- ------e-d-e------ --------e--------
The pattern has the following characteristics:
- Each row is symmetric with dashes for spacing
- The middle row contains all alphabets from 'a' to the nth letter
- Upper and lower halves mirror each other
- Alphabets are separated by dashes
Algorithm
To create this pattern, we follow these steps ?
- Create the upper half (including middle row) by iterating from n-1 down to 0
- For each row, print leading dashes, then alphabets in descending order, then ascending order
- Create the lower half by iterating from 1 to n-1
- Use the same logic as the upper half for symmetry
Example
Let us see the implementation to understand better ?
def solve(n):
# Upper half including middle row
for i in range(n-1, -1, -1):
# Print leading dashes
for j in range(i):
print(end="--")
# Print alphabets in descending order
for j in range(n-1, i, -1):
print(chr(j+97), end="-")
# Print alphabets in ascending order
for j in range(i, n):
if j != n-1:
print(chr(j+97), end="-")
else:
print(chr(j+97), end="")
# Print trailing dashes
for j in range(2*i):
print(end="-")
print()
# Lower half (mirror of upper half)
for i in range(1, n):
# Print leading dashes
for j in range(i):
print(end="--")
# Print alphabets in descending order
for j in range(n-1, i, -1):
print(chr(j+97), end="-")
# Print alphabets in ascending order
for j in range(i, n):
if j != n-1:
print(chr(j+97), end="-")
else:
print(chr(j+97), end="")
# Print trailing dashes
for j in range(2*i):
print(end="-")
print()
# Test with n = 5
n = 5
solve(n)
The output of the above code is ?
--------e-------- ------e-d-e------ ----e-d-c-d-e---- --e-d-c-b-c-d-e-- e-d-c-b-a-b-c-d-e --e-d-c-b-c-d-e-- ----e-d-c-d-e---- ------e-d-e------ --------e--------
How It Works
The algorithm works by dividing the pattern into two parts:
- Upper half: Iterates from n-1 to 0, creating rows with decreasing indentation
- Lower half: Iterates from 1 to n-1, creating the mirror image
-
Character mapping: Uses
chr(j+97)to convert numbers to lowercase letters - Spacing: Uses double dashes for leading spaces and single dashes between characters
Conclusion
The rangoli pattern creates a beautiful diamond shape using alphabets and dashes. The key is to handle the upper and lower halves symmetrically while managing proper spacing and character placement.
