Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,943 questions

55,787 answers

573 users

How to create an alphabet rangoli (geometric and character pattern) of size N in Python

2 Answers

0 votes
# Function to generate and print the alphabet rangoli of size N
def printAlphabetRangoli(n):
    if n <= 0:
        return

    # Total width of the grid based on character and hyphen spacing
    total_width = 4 * n - 3

    # Loop from -(n-1) to (n-1) to handle top/bottom symmetry mathematically
    for i in range(-(n - 1), (n - 1) + 1):
        current_row_dist = abs(i)  # Distance from the center row

        line_chars = ""

        # 1. Build the left/descending side of characters (e.g., e -> d -> c)
        for j in range(0, n - current_row_dist):
            line_chars += chr(ord('a') + n - 1 - j)

        # 2. Build the right/ascending side of characters (e.g., d -> e)
        for j in range(n - current_row_dist - 2, -1, -1):
            line_chars += chr(ord('a') + n - 1 - j)

        # 3. Insert hyphens between characters
        standard_row = ""
        for k in range(len(line_chars)):
            standard_row += line_chars[k]
            if k != len(line_chars) - 1:
                standard_row += "-"

        # 4. Calculate necessary hyphen padding for centering
        total_padding = total_width - len(standard_row)
        side_hyphens = "-" * (total_padding // 2)

        # 5. Print the complete constructed row
        print(side_hyphens + standard_row + side_hyphens)


def main():
    n = 5

    printAlphabetRangoli(n)


main()


"""
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--------

"""

 



answered Jul 13 by avibootz
0 votes
# 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--------

"""

 



answered Jul 13 by avibootz
...