【每日一题1224】圣诞树

:woman_mage: 今天画一棵圣诞树比较应景。

当n=3时的圣诞树:christmas_tree: 如下图:

  *
 ***
*****
 ###

当n=9时的圣诞树:christmas_tree: 如下图:

    *
   ***
  *****
   ***
  *****
 *******
  *****
 *******
*********
   ###

请编写一个函数,只接收一个数字n,代表树的层数,返回打印树形的字符串。

具体的绘画的规则是:

  1. 每棵树都固定有个树根,使用三个# 号表示;
  2. 层数依据输入的n除以3后取整,即int(n/3)

题目难度:中等
题目来源:CodeWars:Merry Christmas

def solution(n: int) -> str:
    # your code

assert solution(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution(8) == "*\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
def solution(n: int) -> str:
    christmas = ''
    pre_num = 1
    space_num = n // 3 + 1
    for _ in range(n // 3):
        christmas += ' ' * space_num + pre_num * '*' + '\r\n' + ' ' * (space_num - 1) \
                     + (pre_num + 2) * '*' + '\r\n' + ' ' * (space_num - 2) + (pre_num + 4) * '*' + '\r\n'

        pre_num = pre_num + 2
        space_num = space_num - 1
    return christmas + ' ' * (n // 3) + "###"


assert solution(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution(8) == "   *\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
def solution(n: int) -> str:
    # 计算层数
    floor = int(n / 3)
    result = ''
    # 遍历层数
    for x in range(1, floor + 1):
        # 定义每层的遍历开始值
        start = 2 * x - 1
        # 定义每层的遍历结束值
        end = 2 * x + 4
        # 开始生成每层的3行字符串,y是每行的*数量,每行的步长为2
        for y in range(start, end, 2):
            # 定义空格数量
            num1 = int((floor * 2 + 3 - y) / 2)
            # 空格数量 + *数量 + 回车换行
            result += ' ' * num1 + '*' * y + '\r\n'
    result += ' ' * floor + '###'
    return result
def solution(n: int) -> str:
    num = int(n / 3)
    str = ''
    for i in range(1, num + 1):
        str += ' ' * (num-(i-1)+1) + '*' * (2 * i - 1) + "\r\n"
        str += ' ' * (num-(i-1)) + '*' * (2 * i + 1) + "\r\n"
        str += " " * (num-(i-1)-1) + "*" * (2 * i + 3) + "\r\n"
    str += " " * num+"###"
    return str
def solution(n: int) -> str:
    # 层数
    num = int(n / 3)
    # 每层数据
    data = []
    # 遍历层数从第1层到num层
    for j in range(1, num + 1):
        # 第j层,每层3行
        for i in range(j, 3 + j):
            # 第i行 num+2-i个空格 2i-1个'*'号
            line = ' ' * (num + 2 - i) + (2 * i - 1) * '*'
            data.append(line)
    # 最后加"###"
    data.append(' ' * num + "###")
    # 格式化处理
    result = '\r\n'.join(data)
    return result


assert solution(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution(8) == "   *\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
def solution8(n):
    # layer为圣诞树层数
    layer=int(n/3)
    y=1
    while y<=layer:
        for i in range((2*y-1),2*y+4,2):
            ss='*'*i
            print(ss.center(2*layer+3))
        y+=1
    print('###'.center(2*layer+3))
solution8(8)
def solution8(n):
    # layer为圣诞树层数
    layer=int(n/3)
    y=1
    christmas=''
    while y<=layer:
        for i in range((2*y-1),2*y+4,2):
            ss='*'*i
            christmas=(christmas+ss.center(2*layer+3)).rstrip()+'\r\n'
            # print(ss.center(2*layer+3))
        y+=1
    return (christmas+'###'.center(2*layer+3)).rstrip()

assert solution8(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution8(8) == "   *\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
def solution(n: int) -> str:
    layer_num = n // 3  # 圣诞树的总层数
    space_num_first = layer_num + 1  # 圣诞树第一层第一行 空格的数量 ,这个必须根据圣诞树总的行数来定
    str = ''   # 最后返回的 圣诞树字符串

    for layer in range(1,layer_num+1):  # 控制圣诞树的层数
        star_num = 2 * layer - 1    # 圣诞树 每一层 第一行的星星数量
        space_num = space_num_first - (layer - 1)   # 圣诞树 每一层 第一行空格的数量
        for n in range(1,4):   # 控制圣诞树的每一层的行数
            str = str+ ' ' * space_num + '*' * star_num + '\r\n'
            star_num = star_num + 2  # 下一行比一行星星多2个
            space_num = space_num -1  # 下一行的空格比上一行的少一个(左边才有空格)
    str = str + ' ' * (space_num_first - 1) + '###'  # 最后的 ### 前面的空格数量,比第一层第一行的空格数量少一个
    return str
def solution(n: int) -> str:
    level = int(n/3)
    max_width = level * 2 + 3
    _list = []
    for cur_level in range(1,level+1):
        for i in range(cur_level*2-1,cur_level * 2 + 4,2):
            _list.append("{:>{width}}".format('*'*i,width=int((max_width-i)/2)+i))
    _list.append("{:>{width}}".format("###", width=level + 3))
    return "\r\n".join(_list)
def solution(n):
    chrimas_tree = ""
    # 获取圣诞树的层数
    tree_floor = int(n/3)

    if tree_floor<1:
        return ""

    for tree_floor_num in range(1, tree_floor+1):
        # 根据圣诞树的层数计算出最顶上的空格数量,第一层space_num space_num-1 space_num-2  第二层为space_num-1 space_num-2 space_num-3
        space_num = tree_floor + 1 - tree_floor_num + 1
        chrimas_tree += " " * space_num + "*" * (2 * tree_floor_num - 1) + "\r\n" + " " * (space_num - 1) + "*" * (
                2 * tree_floor_num + 1) + "\r\n" + " " * (space_num - 2) + "*" * (2 * tree_floor_num + 3)
        if tree_floor_num == tree_floor:
            chrimas_tree +="\r\n" + " "*(tree_floor)+"#"*3
        else:
            chrimas_tree += '\r\n'

    # print(chrimas_tree)
    return chrimas_tree


assert solution(2) == ""
assert solution(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution(8) == "   *\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
def solution(n: int) -> str:
    layer = 1
    str_layer = ''
    for i in range(0, int(n/3)):
        tem_layer = layer
        tem_str = (' '*(2+int(n/3)-1-i) +'*' * tem_layer +'\r\n'+' '*(1+int(n/3)-1-i) + '*' * (tem_layer+2)+'\r\n'+' '*(int(n/3)-1-i)+'*' * (tem_layer+4) + '\r\n')
        str_layer += tem_str
        layer += 2
    return str_layer+' '*(n//3)+'###'
assert solution(5) == "  *\r\n ***\r\n*****\r\n ###"
assert solution(8) == "   *\r\n  ***\r\n *****\r\n  ***\r\n *****\r\n*******\r\n  ###"
关闭