DEV Community

Discussion on: Daily Challenge #169 - Christmas Tree

Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

In Python:

def xmas_tree(height):
  rows = []
  # if height is 5, we count from 1 to 5
  for r in range(1, height+1):
    # append spaces and then *'s:
    #   - number of spaces: directly inverse to row depth
    #   - number of *'s: twice the row depth minus one
    #      (to make an odd total so the center is a '*')
    rows.append(' '*(height-r) + '*'*(r*2-1))
  return '\n'.join(rows)

Demo:

print(xmas_tree(1))
print(xmas_tree(2))
print(xmas_tree(3))
print(xmas_tree(4))
print(xmas_tree(5))
print(xmas_tree(10))
*

 *
***

  *
 ***
*****

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

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

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