Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Python by (50.2k points)

i want to build a hollow diamond using python. Let's say the below is my sample input:

Input an odd Integer:

      9

This is my desired result:

    *

   * *

  *   *

 *     *

*       *

 *     *

  *   *

   * *

    *

However, I was following a similar code written in C, but that is not working properly. Kindly guide me to achieve my output:

a=int(input("Input an odd integer: "))

k=1

c=1

r=a

while k<=r:

    while c<=r:

        print "*"

        c+=1

    r-=1

    c=1

    while c<=2*k-1:

        print "*"

        c+=1

    print "\n"

    k+=1

r=1

k=1

c=1

while k<=a-1:

   while c<=r:

       print " "

       c+=1

   r+=1

   c=1

   while c<= 2*(a-k)-1:

       print ("*")

       c+=1

   print "\n"

   k+=1

The above code is returning the below result:

Input an odd integer: 7

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

*

1 Answer

0 votes
by (108k points)

The main problem with your code is that you are constantly using print(). Kindly be informed that the print statement will add a line-break after what you printed except you explicitly tell it not to. In Python 2, you need to perform like this:

print '*', # note the trailing comma

In the python 3x version, you can use:

print('*', end='')

The correct solution is that I feel should be this:

# The diamond size

l = 9

# Initialize the first row; this will create a list with a

# single element, the first row containing a single star

rows = ['*']

# Add half of the rows; we loop over the odd numbers from

# 1 to l, and then append a star followed by `i` spaces and

# again a star. Note that the range will not include `l` itself.

for i in range(1, l, 2):

    rows.append('*' + ' ' * i + '*')

# Mirror the rows and append; we get all but the last row

# (the middle row) from the list, and inverse it (using

# `[::-1]`) and add that to the original list. Now we have

# all the rows we need. Print it to see what's inside.

rows += rows[:-1][::-1]

# center-align each row, and join them

# We first define a function that does nothing else than

# centering whatever it gets to `l` characters. This will

# add the spaces we need around the stars

align = lambda x: ('{:^%s}' % l).format(x)

# And then we apply that function to all rows using `map`

# and then join the rows by a line break.

diamond = '\n'.join(map(align, rows))

# and print

print(diamond)

This will give you this output:

    *    

   * *   

  *   *  

 *     * 

*       *

 *     * 

  *   *  

   * *   

    *    

Want to become a Python Developer? Check out this insightful Python Certification course.

Related questions

0 votes
1 answer
asked Dec 2, 2020 in Python by laddulakshana (16.4k points)
0 votes
1 answer
asked Feb 17, 2021 in Java by Jake (7k points)

Browse Categories

...