对于作业,我们需要在 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")
}
这给我留下了树的中间和右侧(N = 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
结果竟然是这样!我自己的快乐小(或大,无论你的船漂浮)树。
关于R:圣诞树,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25893689/