Christmas Tree Pattern

 Coding Problem Keys 

 Christmas Tree Pattern 

 Problem Statement 

John is a pure Desi boy. And his one and only dream is to meet Santa Claus. He decided to decorate a Christmas tree for Santa on coming Christmas. John made an interesting Christmas tree that grows day by day.

The Christmas tree is comprised of the following:
    - Parts
    - Stand
Each Part is further comprised of Branches.
Branches are comprised of Leaves.

How the tree appears as a function of days should be understood. Basis that print the tree as it appears on the given day. Below are the rules that govern how the tree appears on a given day. Write a program to generate such a Christmas tree whose input is the number of days.

Rules
1) If the tree is one day old, you cannot grow. Print the message "You cannot generate christmas tree"
2) Tree will die after 20 days. It should give a message "Tree is no more".
3) Tree will have one part less than the number of days.
    E.g. On the 2nd day, the tree will have 1 part and one stand.
            On the 3rd day, the tree will have 2 parts and one stand.
            On the 4th day, the tree will have 3 parts and one stand and so on.
4) Top-most part will be the widest and bottom-most will be the narrowest.
5) Difference in number of branches between top-most and second from the top will be 2.
6) Difference in number of branches between second from top and bottom-most part will be 1.

Below is an illustration og how the tree looks like on 4th day

 Input Format 

The first line contains the number of days.

 Output Format 

The lines contain the Christmas tree or one of the mentioned messages.

 Boundary Condition(s) 

1 <= N
 <= 100

 Example Input/Output 1 

 Input 

4

 Output 

 ----*
 ---***
 --*****
 -*******
*********
 ---***
 --*****
 -*******
 ---***
 --*****
  ----*
  ----*

 Example Input/Output 2 

 Input 

7

 Output 

1
2
1
1
0
2 4

 Max Execution Time Limit: 100 millisecs 

 Solution 

 Programming Language: Python 3 Language 

n=int(input());a=n
if n<=1:
    print('You cannot generate christmas tree')
elif n>20:
    print('Tree is no more')
else:
    for i in range(1,n+2):
        print("-"*(n-i+1),end='')
        for j in range(-i+1,i):
            print('*',end='')
        print()
    while n!=2:
        for i in range(1,n):
            print('-'*(a-i),end='')
            for j in range(-i-1,i):
                print('*',end='')
            print()
        n-=1
    for i in range(2):
        print('-'*(a),end='')
        for j in range(i,i+1):
            print('*',end='')
        print()

# Published By PKJCODERS

 Programming Language: Python 3 Language 

def Head(n):
    for i in range(n+1):
        for j in range(i,n):
            print("-",end="")
        for k in range(0,i+i+1):
            print("*",end="")
        print()
def Branch(k):
    for i in range(1,k):
        for j in range(i,n):
            print("-",end="")
        for k in range(0,i+i+1):
            print("*",end="")
        print()
def Stand(n):
    for i in range(2):
        for j in range(n):
            print("-",end="")
        for k in range(1):
            print("*",end="")
        print()
n=int(input())
k=n
if(n<=1):
    print("You cannot generate christmas tree")
elif(n>20):
    print("Tree is no more")
else:
    for t in range(n):
        if(t==0):
            Head(n)
            pass
        elif(t==n-1):
            Stand(n)
            break
        else:
            Branch(k)
            k-=1

# Published By PKJCODERS

 (Note: Incase If the code doesn't Pass the output kindly comment us with your feedback to help us improvise.) 

Comments

Popular Posts

Maximum Sub Square Sum

Search String S2 in S1 Character Matrix

Largest of Tenth Digit and Unit Digit

ASCII Based Encryption

First N Repeated Characters

Shift Largest Digit - Sum