this post was submitted on 23 Jun 2023
9 points (100.0% liked)

Code Golf

172 readers
1 users here now

Icon base by Delapouite under CC BY 3.0 with modifications to add a gradient

founded 1 year ago
MODERATORS
 

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 are viewing a single comment's thread
view the rest of the comments
[โ€“] gamma 3 points 1 year ago

Just discovered this community existed, I've got to make a post in my usual golfing lang:

Zsh, 70 bytes

l=()
repeat $1 l=(${(l.++i.)}\* $^l' *')
print -l $l ${(l.$1-1.)}'| |'

Try it online!

The key constructs here are (l.ARITHMETIC EXPRESSION.), which left-pads, and $^l, which expands the array like a cross product. a=(a b c); echo x$a prints xa b c, but echo x$^a prints xa xb xc.

Interestingly, print -l was shorter than <<< by one byte.