Tuesday, August 8, 2017

Triangle Pattern - Modified Pascal

Given an integer N, print a triangle as mentioned in the below examples.

Input Format:
The first line contains N.

Output Format:
N lines containing the triangle pattern as shown in the example input/output.

Boundary Conditions:
1 <= N <= 999

Example Input/Output 1:
Input:
4

Output:
      1                                                                                                                                                               
    232                                                                                                                                                               
  34543                                                                                                                                                            4567654

Example Input/Output 2:
Input:
6

Output:
          1                                                                                                                                                             
        232                                                                                                                                                             
      34543                                                                                                                                                            
    4567654                                                                                                                                                          
  567898765                                                                                                                                                         
67891219876

Solution:

n = int(input())
k=0
for i in range(n):
    for j in range(2*n-1):
        if j >= n-i-1 and j<=n-1:
            k+=1
            k = 1 if k==10 else k
            print(k,end='')
        elif j>n-i and j<=n+i-1:
            k-=1
            k = 9 if k==0 else k
            print(k,end='')
        else:
            print(end=' ')
    print()
Share: