How to print following pattern getting issue with spaces?

Viewed 79

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

                            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
3 Answers

Another approximation, by knowing the quantity of spaces in advance using a recursive function:

def findDigits(N):
    if N <= 1:
        return N
    # Changing number to string
    s = str(N)
    # Add length of number to total_sum
    return len(s) + findDigits(N - 1)


def print_inverse_pyramid(n):
    # Calculate number of total digits until n
    total_digits = findDigits(n)
    # Print the pyramid
    for row in range(1, n + 1):
        total_digits -= len(str(row))
        l_r = [str(i) for i in range(row, 0, -1)]
        print(" " * (total_digits + (n - row)) + " ".join(l_r))

print_inverse_pyramid(15)

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 containing how many spaces each number will take up
# in reverse order because the figure is reverse
lst = [' ' * len(str(x)) for x in range(n, 0, -1)]

# go over each number
for x in range(1, n + 1):
    # replace the digit in its place from end
    # by the string represantion of itself
    lst[-x] = str(x)
    # print joined list
    print(' '.join(lst))

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.

Quick and dirty: 'looking' at the last line

n = 15

def serie(n):
    return ' '.join([str(j) for j in range(n, 0, -1)])

maxlen = len(serie(n))
for i in range(1, n +1):
    s = serie(i)
    print(" " * (maxlen - len(s)) + s)

With math, computing the length as the sum of the int of the log10 of values and adding for the spaces

import math

n = 15

def lenserie(n):
    return sum(map(lambda i : int(math.log(i, 10)) + 1 ,range(1, n+1))) + (n-1)

maxlen = lenserie(n)
for i in range(1, n+1):
    print(" " * (maxlen - lenserie(i)) + " ".join([str(i) for i in range(i, 0, -1)]))
Related