5
\$\begingroup\$

I want to print the below pattern

* 
* *
* * *
* * * *
* * * * *

My logic written

for row in range(1,6):
  for col in range(1,6):
    if row is col:
      print(row * '* ')

Can someone review the code and suggest any improvements needed

The code which i have written is it a right approach ?

\$\endgroup\$

1 Answer 1

7
\$\begingroup\$

Welcome to Code Review.

Your code is literally 4 lines. So there isn't much to review.

  • Do not use the is operator to compare the integers. See difference between is and ==

  • You can save the nested loop: you are already ensuring col == row so you can instead write:

for row in range(1,6):
    print("* " * row)
  • If we want to get technical, the code you have written has a time complexity of O(n^3), but the problem can be solved in O(n^2). You can read more on this topic here.
\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.