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

How to print following pattern getting issue with spaces?

I am trying to write the code but not getting how to achieve expected output
Causing issue with space and not able to make proper judgement how to get exact spaces after every iteration

My code :

n=15
cnt=0
lst=[str(' ') for x in range(1,n+1)]
initial_length=len(''.join(lst))
print(initial_length)
for row in range(1,n+1):
  lst[cnt-1]=str(row)
  cnt=cnt-1
  print(' '.join(lst))

Output of above code is not as 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

                            1
                          2 1
                        3 2 1
                      4 3 2 1
                    5 4 3 2 1
                  6 5 4 3 2 1
                7 6 5 4 3 2 1
              8 7 6 5 4 3 2 1
            9 8 7 6 5 4 3 2 1
          10 9 8 7 6 5 4 3 2 1
        11 10 9 8 7 6 5 4 3 2 1
      12 11 10 9 8 7 6 5 4 3 2 1
    13 12 11 10 9 8 7 6 5 4 3 2 1
  14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

Expected output :

                                  1
                                2 1                                                                 
                              3 2 1                                                              
                            4 3 2 1
                          5 4 3 2 1                         
                        6 5 4 3 2 1                       
                      7 6 5 4 3 2 1                     
                    8 7 6 5 4 3 2 1 
                  9 8 7 6 5 4 3 2 1
               10 9 8 7 6 5 4 3 2 1 
            11 10 9 8 7 6 5 4 3 2 1 
         12 11 10 9 8 7 6 5 4 3 2 1
      13 12 11 10 9 8 7 6 5 4 3 2 1
   14 13 12 11 10 9 8 7 6 5 4 3 2 1
15 14 13 12 11 10 9 8 7 6 5 4 3 2 1

>Solution :

You have to account for larger digits taking up more space, which means that when creating the list that contains spaces, you need to multiply the space by how many digits are in that number which you can get by len(str(number)):

n = 15

# create a list that contains spaces of length of how many digits
# will be put there, then reverse it because ascending order and convert
# to list
lst = list(reversed([' ' * len(str(x)) for x in range(1, n + 1)]))

# go over each number
for x in range(1, n + 1):
    # create a copy of the list
    c = lst.copy()
    # create a reversed object containing all the numbers
    # up until x
    numbers = reversed([str(x) for x in range(1, x + 1)])
    # replace the last items in list's copy with the reversed order
    # so basically put the numbers at the end
    c[-x:] = numbers
    # join by space and print
    print(' '.join(c))

Also:
I strongly suggest following PEP 8 – Style Guide for Python Code. Function and variable names should be in snake_case, class names in CapitalCase. Don’t have space around = if it is used as a part of keyword argument (func(arg='value')) but have space around = if it is used for assigning a value (variable = 'some value'). Have space around operators (+-/ etc.: value = x + y(except here value += x + y)). Have two blank lines around function and class declarations. Object method definitions have one blank line around them.

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