Let us see how to print an inverted heart pattern in Python.
Example:
Input: 11
Output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
********* ********
******* ******
***** ****
Input: 15
Output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
************* ************
*********** **********
********* ********
******* ******
Approach:
- Determine the size of the heart.
- Print an inverted triangle with size number of rows.
- Print the rest of the heart in 4 segments inside another loop.
- Print the white space right-triangle at the beginning.
- Print the first trapezium with stars.
- Print the white space triangle.
- Print the second trapezium with stars.
Python3
size = 15
for a in range ( 0 , size):
for b in range (a, size):
print ( " " , end = "")
for b in range ( 1 , (a * 2 )):
print ( "*" , end = "")
print ("")
for a in range (size, int (size / 2 ) - 1 , - 2 ):
for b in range ( 1 , size - a, 2 ):
print ( " " , end = "")
for b in range ( 1 , a + 1 ):
print ( "*" , end = "")
for b in range ( 1 , (size - a) + 1 ):
print ( " " , end = "")
for b in range ( 1 , a):
print ( "*" , end = "")
print ("")
|
Output:
*
***
*****
*******
*********
***********
*************
***************
*****************
*******************
*********************
***********************
*************************
***************************
*****************************
************* ************
*********** **********
********* ********
******* ******
Time complexity: O(S2) for given input size S
Auxiliary Space: O(1)
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!