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 ?

asked Dec 3, 2021 at 4:21
\$\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.
answered Dec 3, 2021 at 5:01
\$\endgroup\$
0

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.