Last active
February 28, 2025 10:39
-
-
Save 1st/a3ef5d60fb2b9e1db754f77686e56840 to your computer and use it in GitHub Desktop.
Print a Christmas tree in Python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Task: Given a number of tree levels. Print a Christmas tree with the given levels, filled by star symbols (*). | |
# Example: | |
# | |
# print_a_tree(20) | |
# | |
# * | |
# *** | |
# ***** | |
# ******* | |
# ********* | |
# *********** | |
# ************* | |
# *************** | |
# ***************** | |
# ******************* | |
# ********************* | |
# *********************** | |
# ************************* | |
# *************************** | |
# ***************************** | |
# ******************************* | |
# ********************************* | |
# *********************************** | |
# ************************************* | |
# *************************************** | |
# Solution: | |
def print_tree_level(level: int, levels: int): | |
spaces = levels - level | |
stars = 2 * (level - 1) + 1 | |
print(' ' * spaces, '*' * stars) | |
def print_a_tree(levels: int): | |
for level in range(1, levels + 1): | |
print_tree_level(level, levels) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment