Challenge: Function that prints a n
big tree
Challenge: Function that prints a n
big tree
Write a function that prints an n
big tree, e.g. for n=5:
*
* *
* * *
* * * *
* * * * *
| |
Here is what I came up with in C:
N,i;a(n){for(i=N=N?N:n+2;i--;printf(i?"* "+(N-n-1<i):--n?"\n":"\n%*s",N,"| |"));n&&a(n);}
// the invocation isn't part of the golf:
main(){a(5);}
PS: Code blocks currently wrap around when they are too long, I've already submitted a patch to make them scroll horizontally instead.
You're viewing a single thread.
My JavaScript solution -
82103 chars:a=n=>{for(s='',i=1;i<=n;i++)s+=' '.repeat(n-i)+'* '.repeat(i)+'\n';console.log(s)}a=n=>{for(s='',i=0;++i<=n;)s+=' '.repeat(n-i)+'* '.repeat(i)+'\n';console.log(s+' '.repeat(n-2)+'| |')}
I'm happy I was able to beat ChatGPT, it had the same strategy but used additional for loops instead of string.repeat(), so it was 113 chars. But I suspect further improvements might be possible with array.reduce or other prototype functions I'm forgetting about.
2 0 Replyvery cool, but you forgot the trunk
3 0 ReplyWoops, can you tell my teachers were always on my case for misreading the assignment :P
4 0 Reply