Loading...
「ツール」は右上に移動しました。
利用したサーバー: wtserver1
5いいね 98 views回再生

Hourglass Pattern in Python | Master Coding Interview Patterns! #coding #python #code #trending #yt

Create an Hourglass Pattern in Python! This pattern is a favorite in coding interviews and helps build your skills with nested loops and alignment. Watch, practice, and get ready for your next technical interview. Don’t forget to subscribe for more Python coding patterns!

def hourglass_pattern(n):
Top half of the hourglass
for i in range(n, 0, -1):
print(" " * (n - i) + "* " * i)

Bottom half of the hourglass
for i in range(2, n + 1):
print(" " * (n - i) + "* " * i)

Input for number of rows
hourglass_pattern(5)


Explanation

Top Half: The first loop decreases the number of stars in each row to create the upper half of the hourglass.

Bottom Half: The second loop increases the stars, creating the bottom half.

Spaces: " " * (n - i) centers each row for the hourglass shape.

コメント