Open In App

Python Program to print hollow half diamond hash pattern

Last Updated : 22 Apr, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

Give an integer N and the task is to print hollow half diamond pattern. Examples:

Input : 6
Output :
# 
# # 
#   # 
#     # 
#       # 
#         # 
#       # 
#     # 
#   # 
# # 
# 

Input : 7
Output :
# 
# # 
#   # 
#     # 
#       # 
#         # 
#           # 
#         # 
#       # 
#     # 
#   # 
# # 
#  

Approach: The idea is to break the pattern into two parts:

  • Upper part: For the upper half start the for loop with iterator (i) from 1 to n and one more for loop with iterator (j) from 1 to i.
# 
# # 
#   # 
#     # 
#       # 
#         # 
#           #  
  • Lower part: For lower half start the for loop with iterator (n-1) to 1 and loop inside this for will remain same as for the upper half.
#         # 
#       # 
#     # 
#   # 
# # 
#
  • Now we have to check this condition that if (i==j) or (j==1) then we have to print “#”, otherwise we have to print ” “(space).

Below is the implementation: 

Python3




# python program to print
# hollow half diamond star
 
 
# function to print hollow
# half diamond star
def hollow_half_diamond(N):
     
    # this for loop is for
    # printing upper half
    for i in range( 1, N + 1):
        for j in range(1, i + 1):
             
            # this is the condition to
            # print "#" only on the
            # boundaries
            if i == j or j == 1:
                print("#", end =" ")
                 
            # print " "(space) on the rest
            # of the area
            else:
                print(" ", end =" ")
        print()
     
    # this for loop is to print lower half
    for i in range(N - 1, 0, -1):
         
        for j in range(1, i + 1):
             
            if j == 1 or i == j:
                print("#", end =" ")
             
            else:
                print(" ", end =" ")
         
        print()
 
# Driver Code
if __name__ == "__main__":
    N = 7
    hollow_half_diamond( N )


Output:

# 
# # 
#   # 
#     # 
#       # 
#         # 
#           # 
#         # 
#       # 
#     # 
#   # 
# # 
# 

Time Complexity: O(N^2)

Space Complexity: O(1) as using constant space

METHOD 2:Using string formatting and list comprehension

APPROACH:

This program prints a hollow half diamond hash pattern of a given size n.

ALGORITHM:

1.Read the value of n from the user.
2.For the upper half of the pattern, use a loop that runs from 1 to n.
3.In each iteration, print a string consisting of a hash (#), followed by zero or more spaces, followed by another hash (#).
4.The number of spaces in each iteration is (i-2), where i is the current iteration number. The first iteration has only one hash, so we need to print a single hash instead of the spaces.
5.Use the rstrip() method to remove any trailing whitespace characters.
6.For the lower half of the pattern, use a loop that runs from n-1 to 1 (in reverse).
7.Use the same logic as in step 3 to print the pattern for the lower half of the diamond.

Python3




n = int(("6"))
  
# Upper half of the pattern
for i in range(1, n+1):
    print(("# " + "  "*(i-2) + ("#" if i>1 else "")).rstrip())
  
# Lower half of the pattern
for i in range(n-1, 0, -1):
    print(("# " + "  "*(i-2) + ("#" if i>1 else "")).rstrip())


Output

#
# #
#   #
#     #
#       #
#         #
#       #
#     #
#   #
# #
#

Time complexity: O(n^2) (quadratic) – because we have two nested loops that run n times each.

Space complexity: O(1) – because we only need a few variables to store the input and intermediate values, and the amount of memory used by the program does not depend on the input size.



Previous Article
Next Article

Similar Reads

Program to print hollow pyramid, diamond pattern and their modifications
For Prerequisite : Loops, If Else Statement1. Hollow pyramid/triangle pattern The pattern is similar to pyramid pattern. The only difference is, we will replace all internal '#' or '*' characters by space character and we will print 2*N-1 (N = number of rows in pattern) '#' or '*' characters in last row. Examples: Input: n=6 Output: # # # # # # # #
15+ min read
Program to print half diamond Number-Star pattern
Given a number N which represents the number of rows. The task is to print a half diamond Number-Star pattern as shown in the below examples.Note: N is always an even number.Examples: Input: N = 4 Output: 2*2 1 1 2*2 Input: N = 6 Output: 3*3*3 2*2 1 1 2*2 3*3*3 On carefully observing the above pattern, it can be broken down into two different trian
6 min read
Program to print half Diamond star pattern
Given an integer N, the task is to print half-diamond-star pattern. ************************************ Examples: Input: N = 3 Output: * ** *** ** * Input: N = 6 Output: * ** *** **** ***** ****** ***** **** *** ** * Approach: The idea is to break the pattern into two halves that is upper half and lower half. Then print them separately with the he
4 min read
Python program to display half diamond pattern of numbers with star border
Given a number n, the task is to write a Python program to print a half-diamond pattern of numbers with a star border. Examples: Input: n = 5 Output: * *1* *121* *12321* *1234321* *123454321* *1234321* *12321* *121* *1* * Input: n = 3 Output: * *1* *121* *12321* *121* *1* * Approach: Two for loops will be run in this program in order to print the n
2 min read
Hollow Half Pyramid Pattern Using Numbers
A hollow half-pyramid pattern using numbers is a type of pattern that seems like a pyramid shape mostly it is considered a star pattern but here we will be creating using numbers.  Program to print the following pattern of a half pyramid for N. Example:  Input: N = 5 1 1 2 1 3 1 4 1 2 3 4 5 Below is the implementation of the above approach: [GFGTAB
8 min read
Program to Print Inverted Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is is to print a left half pyramid pattern with N rows. In this pattern, the first row contains N stars, the second row contains N - 1 stars, and so forth until the Nth row, which contains only 1 star. All the stars are aligned to the right. Examples: Input: 3Output: *** ** * Input: 5Output: ***** **** *** ** * Approach
4 min read
Program to Print Inverted Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of inverted right half pyramid pattern. In inverted right half pattern of N rows, the first row has N number of stars, second row has (N - 1) number of stars and so on till the Nth row which has only 1 star. Examples: Input: n = 5Output:*************** Input: n = 3Output:****** Approach: The problem can be solved us
3 min read
Program to Print Right Half Pyramid Pattern (Star Pattern)
Given an integer N, print N rows of right half pyramid pattern. In right half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are left aligned. Examples: Input: 3Output: ****** Input: 5Output: *************** Approach: The problem can be solved using two nested loops. T
3 min read
Program to Print Left Half Pyramid Pattern (Star Pattern)
Given an integer N, the task is to print N rows of left half pyramid pattern. In left half pattern of N rows, the first row has 1 star, second row has 2 stars and so on till the Nth row which has N stars. All the stars are right aligned. Examples: Input: 3Output: * ***** Input: 5Output: * ** *** ********* Approach: The problem can be solved using t
4 min read
Program to print the pattern with two hollow Triangles
Given a number n, the task is to write a program to draw the following pattern using '$'. $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ Approach: The above pattern is printed by breaking it into smaller patterns: The above triangle $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ $This is
15+ min read