Tuesday, November 19, 2024
Google search engine
HomeLanguagesPython Program to Print Christmas Tree Star Pattern

Python Program to Print Christmas Tree Star Pattern

The “Python program to print Tree pattern” is a piece of code that creates a tree pattern out of asterisks (*) using Python. The indentation and amount of asterisks per row are controlled via loops and string manipulation to produce the tree pattern, which is presented on the console.

Print Christmas Tree Star Pattern using Loops

This program uses nested loops to print out the tree pattern. The outer loop controls the rows of the pattern, and the inner loops control the columns.

The program consists of four sections, each printing a different triangular pattern.

In the first section, a variable num is defined with a value of 3. The program then uses a for loop to iterate from 1 to num+1, which is 1 to 4. Inside the loop, another loop is used to print spaces before the stars, creating a right-aligned triangular pattern. The number of spaces before the stars are calculated using the formula 2*num-i+3, where num is the number of rows and i is the current iteration value of the outer loop. After printing the spaces, the inner loop prints i a number of stars, separated by spaces. 
The second section is similar to the first section but prints num + 2 number of rows instead of num rows. The formula for calculating the number of spaces before the stars are 2*num-i+1.
The third section also prints a triangular pattern, but the number of rows is increased to num + 5. The formula for calculating the number of spaces before the stars are 2*num-i.
The fourth section prints a fixed pattern of three stars in each row, with the stars aligned to the right. The number of spaces before the stars are fixed at 2*num spaces.

Python3




num = 3
for i in range(1, num+1):
    print(" "*(2*num-i+3), end="")
    for j in range(1, i+1):
        print("*", end=" ")
    print()
 
for i in range(1, num+3):
    print(" "*(2*num-i+1), end="")
    for j in range(-1, i+1):
        print("*", end=" ")
    print()
 
for i in range(1, num+5):
    print(" "*(2*num-i), end="")
    for j in range(-2, i+1):
        print("*", end=" ")
    print()
 
for i in range(1, num+3):
    print(" "*((2*num)), end="")
    print("* "*3)


Output:

Python Program to Print Christmas Tree Star Pattern

Terminal Output

Print Christmas Tree Star Pattern using List Comprehension

The code uses list comprehension to generate the string of asterisks for each row of the tree pattern and then joins them with spaces using the Python join() method. The print() function is used to print each row with the appropriate indentation.

Example

The first loop generates the top part of the tree, with increasing indentation and the number of asterisks per row corresponding to the loop variable i. 

The second loop generates the middle part of the tree, with decreasing indentation and the number of asterisks per row also corresponding to the loop variable i. However, there is an issue with this loop where the range for j is from -1 to i+1, which results in negative indices and incorrect asterisk placement. 
The third loop generates the bottom part of the tree, with increasing indentation and the number of asterisks per row starting from -2 and incrementing by 1. 

The fourth loop generates the trunk of the tree, with constant indentation and always printing 3 asterisks per row.

Python3




num = 3
for i in range(1, num+1):
    print(" "*(2*num-i+3) + " ".join("*" for j in range(1, i+1)))
 
 
for i in range(1, num+3):
    print(" "*(2*num-i+1) + " ".join("*" for j in range(-1, i+1)))
 
 
for i in range(1, num+5):
    print(" "*(2*num-i) + " ".join("*" for j in range(-2, i+1)))
 
 
for i in range(1, num+3):
    print(" "*((2*num)) + " ".join("*" for j in range(3)))


Output :

Python Program to Print Christmas Tree Star Pattern

Terminal Output

Time complexity: The time complexity of the given code is O(n^2), where n is the value of the variable ‘num’. This is because the code contains nested loops, each iterating from 1 to ‘num’ or a constant number of iterations (in the case of the last loop). As such, the time complexity is quadratic and increases with the square of the input size.

Space complexity: The space complexity of the given code is O(1) as it does not use any additional data structures that consume memory.

Python Program to Print Christmas Tree Star Pattern using recursion

To print a Christmas tree star pattern using recursion in Python, we can define a recursive function that handles each row of the tree and then call that function recursively to build the entire tree. The star pattern of the Christmas tree will be made up of ‘*’ characters. Here’s a Python program to achieve this:

Python




def print_spaces(count):
    # Helper function to print spaces
    if count > 0:
        print(' ', end='')
        print_spaces(count - 1)
 
def print_stars(count):
    # Helper function to print stars
    if count > 0:
        print('*', end='')
        print_stars(count - 1)
 
def print_tree_row(spaces, stars):
    # Recursive function to print a row of the Christmas tree
    if spaces > 0:
        print_spaces(spaces)
    if stars > 0:
        print_stars(stars)
    print()  # Move to the next line after printing the row
 
def print_tree(rows, current_row=1):
    # Recursive function to print a single Christmas tree
    if current_row > rows:
        return
    spaces = rows - current_row
    stars = 2 * current_row - 1
    print_tree_row(spaces, stars)
    print_tree(rows, current_row + 1)
 
def print_multiple_trees(num_trees, tree_height):
    # Recursive function to print multiple Christmas trees
    if num_trees == 0:
        return
    print_tree(tree_height)
    print()  # Add an empty line between trees
    print_multiple_trees(num_trees - 1, tree_height)
 
if __name__ == "__main__":
    num_trees = 3  # Number of trees to print
    tree_height = 5  # Height of each tree
 
    print_multiple_trees(num_trees, tree_height)


Output:

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

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments