Number 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 number patterns.

problem 1:
print the following pattern.

sample input 1:           
5

sample output 1:
1

2 4

4 8 8

8 16 16 16

16 32 32 32 32

sample input 2:
3

sample output 2:
1

2 4

4 8 8
python code:

n=int(input())
k=1
for i in range(0,n):
    for j in range(0,i+1):
        if j>=1:
            print(k,end=' ')
        elif j<2:
            print(k,end=' ')
            k=k*2
    print('\n') 

problem 2:
sample input :4
sample output:
1

3 2

6 5 4

10 9 8 7
python code:

n=int(input())
k=1
for i in range(n):
    l=[]
    h=''
    for j in range(i+1):
        l.append(str(k))
        k+=1
    l.reverse()
    h=' '.join(l)
    print(h)
    print('\r')   

   
problem 3:
sample input:4
sample output:
1

2 1

4 2 1

8 4 2 1
python code:   

n=int(input())
for i in range(n):
    l=[]
    k=1
    h=''
    for j in range(i+1):
        l.append(str(k))
        k*=2
    l.reverse()
    h=' '.join(l)
    print(h)
    print('\r')

problem 4:
sample input:3
sample output:
1

2 3 4

5 6 7 8 9

python code:

n=int(input())
k=1
for i in range(n):
    for j in range(i*2+1):
        print(k,end=' ')
        k+=1

    print('\n')   

problem 5:
sample input:
sample output:
1       5

  2   4 

    3
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(j+1,end=' ')
        else:
            print(' ',end=' ')
    print('\n')   

problem 6:   
sample input:5
sample output:
        1

      1 2

    1 2 3

  1 2 3 4

1 2 3 4 5
python code:

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

problem 7:
sample input:5
sample output:
1

0 1

1 0 1

0 1 0 1

1 0 1 0 1
python code:

n=int(input())
for i in range(n):
    for j in range(i+1):
        if i==j or (i%2==0 and j%2==0) or (i%2==1 and j%2==1):
            print('1',end=' ')
        else:
            print('0',end=' ')
    print('\n')       

problem 8:
sample input:5
sample output:
1

2 3

4 5 6

7 8 9 10

11 12 13 14 15
python code:

n=int(input())
k=1
for i in range(n):
    for j in range(i+1):
        print(k,end=' ')
        k+=1
    print('\n')   
   

Comments

Popular posts from this blog

MESSAGE sending problem FIXED for all networks and mobiles

star patterns in python

python code for FLAMES