Problem: Print the following pattern for a given n (e.g. n=5 below).
for j in range(i+1, n):
for j in range(-i, i+1):
print s
5
454
34543
2345432
123454321
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+1, n):
s += " "
for j in range(-i, i+1):
s += str(n-abs(j))
print s
Concepts learned: nested loops.
No comments:
Post a Comment