# Python Program to print hollow half diamond hash pattern

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
Next