Open In App

Python Program to print a number diamond of any given size N in Rangoli Style

Last Updated : 31 Dec, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given an integer N, the task is to print a number diamond of size N in rangoli style where N means till Nth number from number ‘1’.

Examples:

Input : 
2
Output :
--2--
2-1-2
--2--

Input : 
3
Output :
----3----
--3-2-3--
3-2-1-2-3
--3-2-3--
----3----

Input :
4
Output :
------4------
----4-3-4----
--4-3-2-3-4--
4-3-2-1-2-3-4
--4-3-2-3-4--
----4-3-4----
------4------

Approach:
First of all print from the first row till middle row, and then print from the row after middle row till the last row. The total number of positions in each row will be 4(N-1) + 1. Number of rows will be 2(N-1) + 1. Time complexity is O(N^2).

Below is the implementation.




def print_diamond(size):
      
    # print the first triangle
    # (the upper half)
    for i in range (size):
          
        # print from first row till
        # middle row
        rownum = i + 1
        num_alphabet = 2 * rownum - 1
        space_in_between_alphabets = num_alphabet - 1
          
        total_spots = (2 * size - 1) * 2 - 1
        total_space = total_spots - num_alphabet
          
        space_leading_trailing = total_space - space_in_between_alphabets
        lead_space = int(space_leading_trailing / 2)
        trail_space = int(space_leading_trailing / 2)
          
        # print the leading spaces
        for j in range(lead_space):
            print('-', end ='')
          
        # determine the middle character
        mid_char = (1 + size - 1) - int(num_alphabet / 2)
          
        # start with the last character 
        k = 1 + size - 1
        is_alphabet_printed = False
        mid_char_reached = False
          
        # print the numbers alternated by '-'
        for j in range(num_alphabet + space_in_between_alphabets):
              
            if not is_alphabet_printed:
                print(str(k), end ='')
                is_alphabet_printed = True
                  
                if k == mid_char:
                    mid_char_reached = True
                  
                if mid_char_reached == True:
                    k += 1
                  
                else:
                    k -= 1
              
            else:
                print('-', end ='')
                is_alphabet_printed = False
          
        # print the trailing spaces
        for j in range(trail_space):
            print('-', end ='')
          
        # go to the next line
        print('')
      
    # print the rows after middle row 
    # till last row (the second triangle 
    # which is inverted, i.e., the lower half)
    for i in range(size + 1, 2 * size):
          
        rownum = i
        num_alphabet = 2 * (2 * size - rownum) - 1
        space_in_between_alphabets = num_alphabet - 1
          
        total_spots = (2 * size - 1) * 2 - 1
        total_space = total_spots - num_alphabet
          
        space_leading_trailing = total_space - space_in_between_alphabets
        lead_space = int(space_leading_trailing / 2)
        trail_space = int(space_leading_trailing / 2)
          
        # print the leading spaces
        for j in range(lead_space):
            print('-', end ='')
          
        mid_char = (1 + size - 1) - int(num_alphabet / 2)
          
        # start with the last char
        k = 1 + size - 1
        is_alphabet_printed = False
        mid_char_reached = False
          
        # print the numbers alternated by '-'
        for j in range(num_alphabet + space_in_between_alphabets):
              
            if not is_alphabet_printed:
                print(str(k), end ='')
                is_alphabet_printed = True
                  
                if k == mid_char:
                    mid_char_reached = True
                  
                if mid_char_reached == True:
                    k += 1
                  
                else:
                    k -= 1
              
            else:
                print('-', end ='')
                is_alphabet_printed = False
          
        # print the trailing spaces
        for j in range(trail_space):
            print('-', end ='')
          
        # go to the next line
        print('')
  
# Driver Code
if __name__ == '__main__':
      
    n = 5
    print_diamond(n)


Output:

--------5--------
------5-4-5------
----5-4-3-4-5----
--5-4-3-2-3-4-5--
5-4-3-2-1-2-3-4-5
--5-4-3-2-3-4-5--
----5-4-3-4-5----
------5-4-5------
--------5--------


Previous Article
Next Article

Similar Reads

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
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
4 min read
Program to print numbers with diamond pattern
write a program where each column represents same number according to given example:Examples : Input : 5 Output : 1 212 32123 212 1 Input : 7 Output : 1 212 32123 4321234 32123 212 1 C/C++ Code // C++ program to print diamond pattern #include<iostream> using namespace std; void display(int n) { // sp stands for space // st stands for number i
6 min read
Program to print Inverse Diamond pattern
Given an integer n, the task is to print the Inverse Diamond Pattern in 2n-1 rows.Example: Input: n = 3 Output: *** *** ** ** * * ** ** *** *** Input: n = 7 Output: ******* ******* ****** ****** ***** ***** **** **** *** *** ** ** * * ** ** *** *** **** **** ***** ***** ****** ****** ******* ******* Approach: The full Inverse Diamond is of 2n-1 row
8 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
Program to print the diamond shape
Given a number n, write a program to print a diamond shape with 2n rows. Examples : C/C++ Code // C++ program to print diamond shape // with 2n rows #include <bits/stdc++.h> using namespace std; // Prints diamond pattern with 2n rows void printDiamond(int n) { int space = n - 1; // run loop (parent loop) // till number of rows for (int i = 0;
11 min read
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
Programs to print Triangle and Diamond patterns using recursion
This article is aimed at giving a recursive implementation for pattern printing. Pattern 1: Example:Input: 5 Output: * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *Implementation: C/C++ Code // Program to print the given pattern #include <iostream> using namespace std; void print_asterisk(int asterisk) { if (asterisk == 0) return;
15+ 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
Program for diamond pattern with different layers
Given a number n and using 0-n numbers you have to print such a pattern.Examples: Input : n = 5 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 4 5 4 3 2 1 0 0 1 2 3 4 3 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0 Input : n = 3 Output : 0 0 1 0 0 1 2 1 0 0 1 2 3 2 1 0 0 1 2 1 0 0 1 0 0 The idea here is to count the space in beginning o
10 min read
three90RightbarBannerImg