star patterns in python

As we all know,python is an easy  to start programming language.
Patterns are the most problems given in hiring interviews.
So,lets get started fully on patterns.
problem 1:
For an input number 'n',print right angle triangle of n columns in '*'.
sample input:5
sample output:
*
* *
* * *
* * * *
* * * * *
python code:
n=int(input())
for i in range(n):
    for j in range(0,i+1):
        print('* ',end='')
    print('\n')

problem 2:
sample input:5
sample output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
python code:
n=int(input('enter integer input\n'))
for i in range(1,n+1):
    print('* '*(2*i-1))

problem 3:
sample input:5
sample output:
*       *

  *   * 

    *   

  *   * 

*       *
python code:
n=int(input())
for i in range(0,n):
    for j in range(n):
        if(i==j or i+j==n-1):
            print('*',end=' ')
        else:
            print(' ',end=' ')
    print('\n')       

problem 4:
sample input:5
sample output:
*       *

  *   * 

    *   
python code:
n=int(input())
for i in range(0,n//2+1):
    for j in range(n):
        if(i==j or i+j==n-1):
            print('*',end=' ')
        else:
            print(' ',end=' ')
    print('\n')   

problem 5:
sample input:5
sample output:
         *

      * *

    * * *

  * * * *

* * * * *
python code:
n=int(input())
for i in range(n):
    for j in range(n):
        if(i+j>=n-1):
            print('*',end=' ')
        else:
            print(' ',end=' ')
    print('\n')   

problem 6:
sample input:5
sample output:
*
* * *
* * * * *
* * * * * * *
* * * * * * * * *
* * * * * * *
* * * * *
* * *
*
python code:
n=int(input())
k=2*n-1
x=k
for i in range(1,k+1):
    if(i<=n):
        print('* '*(2*i-1))
    else:
        print('* '*(x-2))
        x-=2        

problem 7:
sample input:5
sample output:
*
* *
* * *
* * * *
* * * * *
* * * *
* * *
* *
*
python code:
n=int(input())
k=2*n-1
x=n
for i in range(1,k+1):
    if(i<=n):
        print('* '*(i))
    else:
        print('* '*(x-1))
        x-=1

problem 8:
sample input:5
sample output:
    * 

   *  * 

  *  *  * 

 *  *  *  * 

*  *  *  *  *
python code:
n=int(input())
for i in range(n):
    print(' '*(n-i-1),end='')
    for j in range(i+1):
        print('* ',end=' ')
    print('\n')    

Comments

Popular posts from this blog

MESSAGE sending problem FIXED for all networks and mobiles

python code for FLAMES