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.

  • Andy
    link
    fedilink
    arrow-up
    0
    ·
    edit-2
    1 year ago

    EDIT: Damn, lemmy still mangles code. Here’s a gist, and I’ll replace the text in here with images.


    This doesn’t beat anyone’s char count, but here’s my try in Factor. I hope someone comes along and does it even better. 177 chars (1 line):

    code screenshot 1

    You prefix the number to that code to invoke.

    Here it is with the slight bit of sanity afforded by line breaks and a function wrapper:

    code screenshot 2

    To invoke:

    5 tree print 
    

    EDIT 2: it’s 176 if you replace first2 with last2, as someone pointed out to me.