Alphabet patterns in python
problem 1:
sample input:5
sample output:
A
B C
D E F
G H I J
K L M N O
python code:
import string
x=string.ascii_uppercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 2:
sample input:5
sample output:
a
b c
d e f
g h i j
k l m n o
python code:
import string
x=string.ascii_lowercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 3:
sample input:5
sample output:
A
B B
C C C
D D D D
E E E E E
python code:
import string
x=string.ascii_uppercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 4:
sample input:5
sample output:
a
b b
c c c
d d d d
e e e e e
python code:
import string
x=string.ascii_lowercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 5:print using format specifiers
sample input:5
sample output:
A
B B
C C C
D D D D
E E E E E
python code:
import string
x=string.ascii_uppercase
n=int(input())
for i in range(1,n+1):
print('{} '.format(x[i-1])*i)
problem 6:
sample input:5
sample output:
A
C B
F E D
J I H G
O N M L K
python code:
import string
n=int(input())
x=string.ascii_uppercase
k=0
for i in range(n):
l=[]
h=''
for j in range(i+1):
l.append(x[k])
k+=1
l.reverse()
h=' '.join(l)
print(h)
problem 7:
sample input:5
sample output:
a
b c
d e f
g h i j
k l m n o
python code:
import string
n=int(input())
k=0
x=string.ascii_lowercase
for i in range(n):
for j in range(n):
if(i+j>=n-1):
print(x[k],end=' ')
k+=1
else:
print(' ',end=' ')
print('\r')
sample input:5
sample output:
A
B C
D E F
G H I J
K L M N O
python code:
import string
x=string.ascii_uppercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 2:
sample input:5
sample output:
a
b c
d e f
g h i j
k l m n o
python code:
import string
x=string.ascii_lowercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 3:
sample input:5
sample output:
A
B B
C C C
D D D D
E E E E E
python code:
import string
x=string.ascii_uppercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 4:
sample input:5
sample output:
a
b b
c c c
d d d d
e e e e e
python code:
import string
x=string.ascii_lowercase
k=0
n=int(input())
for i in range(n):
for j in range(i+1):
print(x[k],end=' ')
k+=1
print('\r')
problem 5:print using format specifiers
sample input:5
sample output:
A
B B
C C C
D D D D
E E E E E
python code:
import string
x=string.ascii_uppercase
n=int(input())
for i in range(1,n+1):
print('{} '.format(x[i-1])*i)
problem 6:
sample input:5
sample output:
A
C B
F E D
J I H G
O N M L K
python code:
import string
n=int(input())
x=string.ascii_uppercase
k=0
for i in range(n):
l=[]
h=''
for j in range(i+1):
l.append(x[k])
k+=1
l.reverse()
h=' '.join(l)
print(h)
problem 7:
sample input:5
sample output:
a
b c
d e f
g h i j
k l m n o
python code:
import string
n=int(input())
k=0
x=string.ascii_lowercase
for i in range(n):
for j in range(n):
if(i+j>=n-1):
print(x[k],end=' ')
k+=1
else:
print(' ',end=' ')
print('\r')
Comments
Post a Comment