# Alphabet Rangoli Generator
# --------------------------
# This program prints a geometric pattern built from letters.
# The pattern uses the first N letters of the alphabet in reverse order,
# forming a symmetric rangoli shape.
def build_line(n, i):
"""
Build a single line of the rangoli.
n : total size
i : index of the current line (0-based)
The line is constructed by:
1. Selecting letters from the alphabet starting at position n-1 down to n-1-i.
2. Mirroring them to create symmetry.
3. Joining with hyphens.
4. Padding with hyphens to center the pattern.
"""
# Compute the letters for the descending part
# Using ASCII arithmetic to convert numbers to characters
letters_desc = [
chr(ord('a') + (n - 1 - j)) # convert number to letter
for j in range(i + 1)
]
# Mirror the descending part (excluding the last element to avoid duplication)
letters_full = letters_desc + letters_desc[-2::-1]
# Join letters with hyphens
line = "-".join(letters_full)
# Compute total width of the rangoli
total_width = 4 * n - 3
# Center the line with hyphens
return line.center(total_width, "-")
def print_rangoli(n):
"""
Print the full rangoli pattern of size n.
The pattern consists of:
- Upper half (including middle)
- Lower half (mirror of upper half)
"""
# Upper half
for i in range(n):
print(build_line(n, i))
# Lower half (mirror)
for i in range(n - 2, -1, -1):
print(build_line(n, i))
# Run the rangoli for N = 5
print_rangoli(5)
"""
run:
--------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--------
"""