Problem: Print the following pattern for a given n (e.g. n=5 below).
A A
B B
C C
D D
EE
A A
B B
C C
D D
EE
Solution in Python:
def print_pattern(n):
"""
Author: Mayur P Srivastava
n is number of colmns or rows.
"""
for i in range(n):
s = ""
for j in range(i):
s += " "
s += chr(ord('A') + i)
for j in range(-(n-i-1), (n-i-1)):
s += " "
s += chr(ord('A') + i)
print s
Concepts learned: nested loops.
No comments:
Post a Comment