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

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

Creating a number diamond in a Rangoli style with a given size N involves printing numbers in a specific pattern. The Rangoli pattern typically has a symmetrical structure with numbers increasing from 1 to N in the upper half and then decreasing back to 1. This pattern is mirrored in the lower half.

Let's create a Python program to achieve this. The program will take an input N and print a diamond shape where the widest line has numbers from 1 to N and then back down to 1.

Here's how you can write this program:

def print_rangoli_number_diamond(N): # The total width of the diamond will be 2 * N - 1 width = 2 * N - 1 # Upper half of the diamond for i in range(1, N + 1): # Creating the number sequence for the current row numbers = list(range(1, i)) + list(range(i, 0, -1)) # Joining the numbers into a string with '-' in between row = '-'.join(map(str, numbers)).center(width * 2 - 1, '-') print(row) # Lower half of the diamond for i in range(N - 1, 0, -1): # Creating the number sequence for the current row numbers = list(range(1, i)) + list(range(i, 0, -1)) # Joining the numbers into a string with '-' in between row = '-'.join(map(str, numbers)).center(width * 2 - 1, '-') print(row) # Example usage N = int(input("Enter the size of the diamond (N): ")) print_rangoli_number_diamond(N) 

In this program:

  • We iterate over a range of numbers to create each row of the diamond.
  • For the upper half, the range goes from 1 to N, and for the lower half, it goes from N−1 to 1.
  • We use list comprehension and string methods to construct each row.
  • The center method is used to align the numbers correctly to form the diamond shape.

More Tags

named-pipes ansible-handlers melt checkout access-control antiforgerytoken lookup odbc lucene function-call

More Programming Guides

Other Guides

More Programming Examples