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.

  • xthexder
    link
    fedilink
    arrow-up
    4
    ·
    edit-2
    1 year ago

    Here’s my attempt with Rust:

    101 Characters:

    fn a(n:usize){for i in 1..=n{println!("{:2$}{}","","* ".repeat(i),n-i);}println!("{:1$}| |","",n-2);}
    

    Rust Playground Link

    (Edited to remove a few unnecessary whitespace characters)