Problem: Print the following pattern for a given n (e.g. n=5 below).
*
**
***
****
*****
Solution in Python:
def print_pattern(n):
for i in range(n):
s = ""
for j in range(i+1):
s += "*"
print s
*
**
***
****
*****
Solution in Python:
def print_pattern(n):
"""
n is number of colmns or rows.
"""
for i in range(n):
s = ""
for j in range(i+1):
s += "*"
print s
Concepts learned: nested loops.
No comments:
Post a Comment