R:圣诞树

Nas*_*oen 5 tree r

对于作业,我们需要在R画一棵圣诞树.我搜索了互联网,发现了一些有用的建议,但最终,我不知道如何继续,希望有人可以帮助我.

到目前为止这是我的代码.

#ctree: prints a Christmas tree on screen with size N
ctree <- function(N){
for (i in 1:N){
    width = sample("*",i,replace=T)
    cat(width,sep="-","\n")
    }   
cat(width[1],"\n")
}
Run Code Online (Sandbox Code Playgroud)

这让我看到了树的中间和右侧(N = 4),这很棒,但还不够.

*-
*-*-
*-*-*-
*-*-*-*-
*
Run Code Online (Sandbox Code Playgroud)

我计划颠倒我所拥有的(基本上右对齐函数的产品)来创建左侧,随后删除左侧最右边的列并将其与树的右侧粘合在一起,创建一个圣诞树.

我真的希望有人可以帮我实现这个目标!期待您的建议.

提前致谢.

Nas*_*oen 4

对于任何感兴趣的人:这就是我最终在 R 中创建圣诞树的过程。

#ctree: prints a Christmas tree on screen with amount of branch levels N
ctree <- function(N){
filler = "*"
blank = ""

for (i in 1:N){
    row = c(sample(blank,N-i,replace=T),sample(filler,i,replace=T),sample(blank,N-i,replace=T))
    cat(row,"\n")
    }   
cat(c(sample(blank,(N-1),replace=T),sample(filler,1,replace=T),sample(blank,(N-1),replace=T)),"\n")
} #ctree
Run Code Online (Sandbox Code Playgroud)

这就是结果!我自己的快乐小树(或大树,随你的船而定)。

在此输入图像描述