Printing Simple Diamond Pattern in Python

Printing Simple Diamond Pattern in Python

Since the middle and largest row of stars has 9 stars, you should make n equal to 9. You were able to print out half of the diamond, but now you have to try to make a function that prints a specific number of spaces, then a specific number of stars. So try to develop a pattern with the number of spaces and stars in each row,

Row1: 4 spaces, 1 star, 4 spaces
Row2: 3 spaces, 3 stars, 3 spaces
Row3: 2 spaces, 5 stars, 2 spaces
Row4: 1 space, 7 stars, 1 space
Row5: 0 spaces, 9 stars, 0 spaces
Row6: 1 space, 7 stars, 1 space
Row7: 2 spaces, 5 stars, 2 spaces
Row8: 3 spaces, 3 stars, 3 spaces
Row9: 4 spaces, 1 star, 4 spaces

So what can you deduce? From row 1 to (n+1)/2, the number of spaces decreases as the number of stars increase. So from 1 to 5, the # of stars = (row number * 2) - 1, while # of spaces before stars = 5 - row number.

Now from row (n+1)/2 + 1 to row 9, the number of spaces increase while the number of stars decrease. So from 6 to n, the # of stars = ((n+1 - row number) * 2) - 1, while # of spaces before stars = row number - 5.

From this information, you should be able to make a program that looks like this,

n = 9
print("Pattern 1")
for a1 in range(1, (n+1)//2 + 1): #from row 1 to 5
for a2 in range((n+1)//2 - a1):
print(" ", end = "")
for a3 in range((a1*2)-1):
print("*", end = "")
print()

for a1 in range((n+1)//2 + 1, n + 1): #from row 6 to 9
for a2 in range(a1 - (n+1)//2):
print(" ", end = "")
for a3 in range((n+1 - a1)*2 - 1):
print("*", end = "")
print()

Note that you can replace n with any odd number to create a perfect diamond of that many lines.

How to print this diamond pattern using python?

I'd do a function for one line, and run it twice, skipping the 4 at the end:

def oneline(i):
for j in range(i,10):
print(' ', end='')
for j in range(1, i+1):
print(j if j<10 else 1, end='')
for j in range(i-1, 0, -1):
print(j if j<10 else 1, end='')
print()

for i in range(1,11):
oneline(i)
for i in range(9,0,-1):
if i == 4:
continue
oneline(i)

Output:

         1
121
12321
1234321
123454321
12345654321
1234567654321
123456787654321
12345678987654321
1234567891987654321
12345678987654321
123456787654321
1234567654321
12345654321
123454321
12321
121
1

How to write a program to print the hollow diamond pattern similar to the pattern shown?

There are 2 things we need to consider. Lets say the number given was 30, this means the alphabets should round around and repeat. When we have this situation, we need a modulus which is % in python.

The other thing is how do we centre the string? In python, there's a string method called center which takes an integer specifying the length of the centered string and also an optional argument which is set to space which is what we need.

Looking at the diamonds you have in the question, we can observe a pattern. The max length is the nth odd number which can be found with this formula: Nth odd number = N*2-1

This is the same for the number of spaces between the 2 characters in each row.

How do we get the characters? We can do this easily with the chr function which converts ascii character values to an actual character. Looking at the ascii table, capital alphabets start with code 65. Using this we can create this code for handling the alphabets. chr(n%26+65)

So, here's the code:

row = int(input(': '))

n = row*2-1 # The length to center the string with.

# Upper part of diamond
for i in range(row):
if i == 0: # if it is the first iteration, then print out 1 character only centered
print('A'.center(n))
continue # skip this iteration. essentially skipping the latter code.
character = chr(i%26+65) # the character to be used for the sides.
print(f"{character}{' '*(2*i-1)}{character}".center(n)) # printing the line itself. if you didn't know, you can multiply strings with integers to result in that character/string being repeated integer times.

# Lower part of the diamond. Same thing as above, but mirrored.
for i in range(0, row-1)[::-1]:
if i == 0:
print('A'.center(n))
continue
character = chr(i%26+65)
print(f"{character}{' '*(2*i-1)}{character}".center(n))

That's all! Happy coding!



Related Topics



Leave a reply



Submit