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.
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)
Comments
Leave a comment