Code-golfing FizzBuzz

The most frustrating is when an idea seems potentially it then realize you’re at 61+ char/bytes haha.

If the approach is different share it even though is not the shortest one. Someone else could find a way to make it work

The primary different approach I have tried is to use something like
"$x FizzBuzz"[start condition:end condition] or another variation because besides using max to compare strings "$x" and "Fizz^(x%x<1)*"Buzz"^(x%5<1) not sure how using "$x" would help–given that at least one of the 59char/byte solutions involves using that interpolation.

the problem is that “$x” length will change, so a unique start condition is tricky…

on that line I have a 64

/ =gcd;1:100 .|>n->println(max("FizzBuzz"[7-2(3/n):3+5/n],"$n"))
4 Likes

Yeah that’s why I abandoned that approach.

That is a neat solution

putting together various ideas that appeared in previous posts …

 1:100 .|>i->println("$i\r","fizz"^(i%3<1),"buzz"^(i%5<1))
2 Likes

oddly this type of solution works correctly in the Julia REPL but if plugging in here Fizz Buzz it doesn’t.

in fact, it seemed too within the reach of previous posts to be overlooked

this is printing both digits and “fizz”/“buzz” instead of skipping either

also, code.golf checks the solution verbatim, thus \r != \n

it would have been nice to beat the record by 2 tho! :smiley:

So I know there is a 60 char/byte solution that uses repr and I got a 61byte/char so maybe analyzing it can give a hint how they got the 60 byte/char.
x=1:100;@.println(max("Fizz"^(1>x%3)*"Buzz"^(1>x%5),repr(x)))

how do you know that such solution exists?

See this previous post I made

1 Like

Not less, more infact, 67 char/byte but one I like
1:100 .|>x->max(("Fizz","Buzz").^(x.%(3,5).<1)|>prod,"$x")|>println

Found a way to use 99, 65/63

0:99 .|>i->println(max("$(i+1)","Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))

and 64/62

i=0:99;@.println(max(repr(i+1),"Fizz"^(i%3÷2)*"Buzz"^(i%5÷4)))
2 Likes

Couple fun solutions
73Byte/Char
\ =(x,y)->x%y<1;x=1:100;@.max("Fizz"^(x\3)*"Buzz"^(x\5),repr(x))|>println
72Byte/Char
\ =(x,y)->x%y<1;1:100 .|>x->max("Fizz"^(x\3)*"Buzz"^(x\5),"$x")|>println

And got rid of max but not for a shorter solution
68Byte/Char
1:100 .|>x->"Fizz"^(x%3<1)*"Buzz"^(1>x%5)*"$x"^(x%3>0<x%5)|>println

Improved this to 64Byte/Char
i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0

3 Likes

can you explain step by step how this function works?

i<n=102>i+1<println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i"));1<0

Not for the println(...) part which is “simple”, but for the i<n=102>i+1< ... ;1<0 part

i<n=...

overloads < operator but it keeps its precedence. We could have written f(i,n)=...

Here is where the thing gets interesting

i<n=102>i+1<...

The first part, 102>i+1, is a comparison, the second is a function call! Let’s rewrite it as

function f(i, n)
    if 102>i+1
        f(i+1, println(max("Fizz"^(1>i%3)*"Buzz"^(1>i%5),"$i")))
    end
end

as soon as 102>i+1 == false the execution stops. In all the other cases, we call again our function with an increased i and we give the println(...) as the n argument. The println statement is ran at f call time, so it doesn’t really matter what type that argument is.

The magic happens thanks to the chaining properties of < and >. It’s perfectly legit julia code to compare the same integer with two boundaries as 10 < x > 3, as this translates into 10 < x && x > 3.

Finally

...;1<0

launches the recursive function. The first argument is important as it’s your index I, the second one is not that interesting but you need something so 0 it is.

hope it makes sense

8 Likes

thank you very much.
this was the part i was missing…

PS
also need a “;” at the end to avoid having a “false” at the end of the output!?

1 Like

the ; is used to indicate the end of the function. That’s simply to inline the solution rather than having

i<n=...
1<0

both solutions have the same number of bytes, but I find easier to keep everything on a single line.

If instead you are referring to a ; after 1<0, that is not needed as that’s only to avoid the REPL to print the function return argument. In code.golf they use the Julia interpreter and read from stdout. This behaviour can sometimes be abused (e.g. some solutions may stop execution with an error, but that doesn’t show in stdout :stuck_out_tongue: )