Answer to Question #237556 in Python for kil

Question #237556

Given an integer number N as input. Write a program to print the double triangular pattern of N lines using an asterisk(*) character as shown below.


1
Expert's answer
2021-09-15T03:26:06-0400


def printx(n):
 
    for i in range(1, n + 1):
        if (i % 2 != 0):
            print("x ", end = "")
        else:
            print("o ", end = "")
 
    return
 


def printo(n):
 
    for i in range(1, n + 1):
        if (i % 2 != 0):
            print("o ", end = "")
        else:
            print("x ", end = "")
 
    return
 


def printPattern(n):
 
    
    x = n
 
    if (n % 2 == 0):
        x = x - 1
 
    
    p = n - 1
 
   
    s = 1
 
    
    for i in range(1, (x - 1) // 2 + 1):
        for j in range(1, p + 1):
            print(" ", end = "")
 
        if (i % 2 != 0):
            printx(s)
        else:
            printo(s)
 
        print()
        p += 1
 
        for j in range(1, p + 1):
            print(" ", end = "")
 
        if (i % 2 != 0):
            printx(s)
        else:
            printo(s)
 
        print()
 
        p -= 1
        s += 1
 
    
    if (n % 2 == 0):
        for i in range(1, p + 1):
            print(" ", end = "")
 
        if (n % 4 != 0):
            printx(n // 2)
        else:
            printo(n // 2)
 
        print()
 
    
    if (n % 2 != 0):
        printx(n)
    else:
        if (n % 4 != 0):
            printx(n // 2)
            printx(n // 2)
        else:
            printx(n // 2)
            printo(n // 2)
 
    print()
 
    
    if (n % 2 == 0):
        print(" ", end = "")
        printx(n // 2)
        print()
 
    
    p = 1
 
    if (n % 2 == 0):
        x-=1
        p = 2
 
    q = x // 2
 
    
    for i in range(1, x + 1):
        for j in range(1, p + 1):
            print(" ", end = "")
 
        printx(q)
 
        if (i % 2 == 0):
            q -= 1
 
        print()
 
        p += 1
 
    print()
 
# Testing code
n = 2
printPattern(n)

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS