Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not able to remove extra star from pattern

I have pattern which I am trying to print

Expected output :

1 * 2 * 3 * 4 * 5 
1 * 2 * 3 * 4 
1 * 2 * 3 
1 * 2 
1 

I have written a code but not getting how to add this check so that last star should not be printed [ refer above expected output ]

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

n=5
l = [  str(i) + ' *'   for i in range(1,n+1) ]
for j in range(1,n+1):
  print(' '.join(l))
  del l[-1]

My output :

1 * 2 * 3 * 4 * 5 *
1 * 2 * 3 * 4 *
1 * 2 * 3 *
1 * 2 *
1 *

>Solution :

Here’s what I got:

n=5
l = [ str(i)  for i in range(1,n+1) ]
for _ in range(1,n+1):
  print(' * '.join(l))
  del l[-1:]

I essentially just removed the star in the list and used it as a separator.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading