DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #169 - Christmas Tree

Create a function christmasTree(height) or christmas_tree(height) (in Ruby) that returns a Christmas tree of the correct height

christmasTree(5) || christmas_tree(height) should return:

Alt Text

Height passed is always an integer between 0 and 100.

Use \n for newlines between each line.

Pad with spaces so each line is the same length. The last line having only stars, no spaces.


This challenge comes from user8062788 on CodeWars. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!

Want to propose a challenge idea for a future post? Email yo+challenge@dev.to with your suggestions!

Top comments (4)

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))
*

 *
***

  *
 ***
*****

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

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

         *
        ***
       *****
      *******
     *********
    ***********
   *************
  ***************
 *****************
*******************
Collapse
 
nijeesh4all profile image
Nijeesh Joshy • Edited
def christmas_tree(height = 0)
  star_string = ""
  height.times do |level|
    number_of_stars = 2 * (level + 1) - 1
    stars = '*' * number_of_stars
    star_string << " " * (height - level)  << stars << "\n"
  end
  star_string
end

print christmas_tree(5)
print christmas_tree(10)
Collapse
 
savagepixie profile image
SavagePixie

In JavaScript

const christmasTree = height => {
  const width = height * 2 - 1
  return Array
    .from(
      { length: height },
      (v, i) => {
        const stars = i + 1
        return '*'.repeat(stars * 2 - 1)
          .padStart(width / 2 + stars)
          .padEnd(width)
      }
    )
    .join('\n')
}
Collapse
 
kerldev profile image
Kyle Jones

Forgot to post my soution from yesterday, also in Python:

import sys


def christmas_tree(height):
    '''
    Outputs a Christmas tree of the correct height.
    '''
    try:
        height = int(height)
    except:
        raise TypeError('Height must be of type int. Actual type: {}'.format(type(height)))
    if height is None or height < 1 or height > 100:
        raise ValueError('Height must contain a value greater than 0')

    level = 0
    while level < height:
        stars = '*' * calculate_num_of_stars(level)
        stars = stars.center(height * 2)
        print(stars)
        level += 1


def calculate_num_of_stars(level):
    '''
    Calculates the correct number of stars that must be output.
    '''
    return 2 * (level + 1) - 1


if len(sys.argv) > 1:
    height = sys.argv[1]
    print(christmas_tree(height))