Code-golfing FizzBuzz

Just realised there is no Julia soluton to this on Code golf code golf - 1, 2, Fizz, 4, Buzz - Code Golf Stack Exchange

2 Likes

Thatā€™s a very weird discussion, dominated by esoteric programming languages.

Incidentally, do people really ask for this in an interview? I would imagine that simply looking at code on someoneā€™s public FOSS contributions would be a much quicker and more specific test. If a significant number of interviewees are filtered out by this in a process, then the interviewers are not using their own time efficiently.

3 Likes

Iā€™ll bite. Hereā€™s a moderately respectable 69 chars:

(i->(f=i%3ā‰ˆ0;b=i%5ā‰ˆ0;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100);
7 Likes

I just submitted some results to code golf for laughs:

Julia 87 bytes

z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
println.(z.(1:100))

Or we could kind of cheat to drop 3 bytes and use show instead of println.
Julia 83 bytes

z(i)=(f=i .%[3,5] .==0;sum(f)>0 ? foldl(*,["Fizz","Buzz"][f]) : i)
show(z.(1:100))

or a more legible FP style, poor performing, 91 bytes

F(x)=foldl(*,["Fizz","Buzz"][x .%[3,5] .==0])
N(y)=F(y)=="" ? y : F(y)
println.(N.(1:100))

Looks like you beat me with 75 bytes! Nice one! Maybe swap out println with show?

maybe this counts :stuck_out_tongue:
71 bytes

(i->(f=i%3ā‰ˆ0;b=i%5ā‰ˆ0;show("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

Isnā€™t a FizzBuzz code supposed to give its output in precisely this form?

Think so. But, I dunno if they care if some other things like " 's are in there or not.

My reading of their rules were that we needed to print to stdout in the standard format so I think show is out :slight_smile:

I missed that they were scoring in bytes rather than characters. Thatā€™s a bit of a disappointment, it reduces the golfing opportunities a bit. In that case we may as well use == rather than ā‰ˆ for a total of 71 bytes (also dropping the trailing ; if not using it at the REPL.)

(i->(f=i%3==0;b=i%5==0;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

There may be other options. An observation related to one of the python solutions is that "FizzBuzz$i"[n:m] would do the trick for the right n and m.

5 Likes

You can do ā€˜i%3<1ā€™ for 2 bytes.

3 Likes

Awe well I tried! Awesome solution though!

I also submitted some code golfs to a few other simple ones. I know Julia isnā€™t made to do this sort of thing, and itā€™s really an obtuse use of a programming language - but itā€™s kind of fun and could give some exposure.

I also made a code golf:

1 Like

I find it interesting that the languages that ā€œwinā€ by the code length criterion range from the somewhat obscure to the very esoteric.

OTOH, the ā€œverboseā€ languages are actually those that most people prefer to program in, especially for nontrivial projects.

There may be a lesson here :wink:

3 Likes

Not sure if people prefer Java or they just learnt it at uni or forced to use it at work.

2 Likes

I couldnā€™t agree more. Most of those languages make me sick to my stomach! It is interesting to see some of the solutions in the sane languages with usable syntax though.

If I were ever hiring someone and they actually wrote something in brainfuck, hexagony, labrynth, or whatever the hell I would have serious concerns about their ability to color within the lines. I had a running gag at a previous job that if I were ever interviewing someone and they said the word ā€œMortranā€ I would immediately close the interview and leave the room.

2 Likes

I have the opposite feeling. I thikk they are pretty smart. But jot sure if they have the patience to do boring work like colouring within the lines.

Oh yes of course! So we have the following for 69 bytes:

(i->(f=i%3<1;b=i%5<1;println("Fizz"^f*"Buzz"^b*"$i"^!(f|b)))).(1:100)

It seems likely a different approach would be needed to make much more progress.

Abusing the tools is the fun of it! I really wanted to use more broadcast in the solution like your nice solution where you have foldl and i .%[3,5] .==0 but the reduction consumes a lot of characters. Hereā€™s a related 78 byte solution:

(i->(f=i.%[3,5].<1;println(join(["Fizz","Buzz","$i"][[f;sum(f)<1]])))).(1:100)
2 Likes

I donā€™t think that any of those esoteric languages (eg Keg, Mouse 2002, etc) are used to write anything nontrivial (I am not talking about production, just something one would do in 100 LOC in Julia). They may be fun to play with, but I am not sure even their inventors would argue that they would be an improvement over even the dullest mainstream languages.

I can only manage one letter shorter than yours, for 68 (letters, but not bytes) not including the semicolon needed for the REPL:

julia -e '(i->(x="Fizz"^(i%3ā‰ˆ0)*"Buzz"^(i%5ā‰ˆ0);println(x>"" ? x : i))).(1:100)'

Right (I did too) but you can avoid == and use <, and with ā‰ˆ, this cryptic code doesnā€™t make up for it:

for i = 1:100 f,b=.ā‰ˆ((i%3,i%5), 0);println("Fizz"^f*"Buzz"^b*"$i"^!(f|b))end

I canā€™t take full credit for my best solution, as I combined bits from yours and what was actually there in this answer from 2015 from Alex A (which no longer works in current Julia, I had to add spaces, then actually getting 68 bytes, not just letters):

Julia, 64 bytes

for i=1:100 x="Fizz"^(i%3<1)*"Buzz"^(i%5<1);println(x>""?x:i)end

EDIT: Off-topic below, just cool solutions for Julia to beat (but I doubt possible, still some solutions use gcd or square root, so possibly āˆš in Julia typed \sqrt helps):

Even PHP is hard to beat and I like how they use ƶ from my Icelandic (or e.g. German) alphabet:

PHP, 54 bytes

<?for(;$i++<100;)echo[Fizz][$i%3].[Buzz][$i%5]?:$i,~Ƶ;

Valid for v5.5 onwards. The Ƶ is character 245, a bit inverted \n .

and also:

Python 2, 56

i=0;exec"print i%3/2*'Fizz'+i%5/4*'Buzz'or-~i;i+=1;"*100

Let alone Perl at 45 bytes, or (other) made for golfing languages:

MathGolf, 24 22 bytes

ā™€{Ć®ā••Ī£ā• Ī“ā••ā”Œā• Ī“`+Ī“Ć®35Ī±Ć·Ć¤Ā§p

or those that cheat, here by language targeting this problem:

gs2, 1

f

Update : Added 27-byte answer that doesnā€™t use f

My favorite newly discovered (after Piet, and Julia) language/solution is though:

Hexagony, 77 76 bytes

=?3})"F\>%'5?\"\B;u;"}/4;d'%<>\g/"!{{\}.%<@>'...<>,}/${;;z;i;z;;$/<>?{$/"./_

Try it online!

Same side length as M Lā€™s solution, but a bit smaller.

answered Mar 24 '18 at 11:40



Jo King

Hexagony, 91 bytes

[ā€¦] Fizzbuzz in a size 6 hexagon:

3}1"$.!$>)}g4_.{$'))\<$\.\.@\}F\$/;z;u;<%<_>_..$>B/<>}))'%<>{>;e"-</_%;\/{}/>.\;.z;i;..>(('

[ā€¦] So, here is a 110 seconds long GIF animation at 2 fps, showing the program flow during the first 6 numbers 1, 2, Fizz, 4, Buzz, Fizz , the first 220 ticks of the program (click on the image for the full size):

enter image description here

[ā€¦] Hexagony is not that hard to work with. But golfing it can be a pain in the butt. :wink:

It was fun!

answered Mar 5 '16 at 21:56



M L

I can read non-Julia solutions on stackoverflowā€¦

I got it down to 67 bytes with both the for-loop implementation and the iterator implementation, but Iā€™m not seeing a way forward from here either:

for i=1:100;f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i)end
(i->(f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i))).(1:100)

Note that doing f,b=i%3<1,i%5<1 is the same number of characters as f=i%3<1;b=i%5<1.

Lots of variations on the same theme that donā€™t actually shorten it at all:

g(i,f=i%3<1,b=i%5<1)=println(b|f ? "Fizz"^f*"Buzz"^b : i);g.(1:100)
(g(i,f=i%3<1,b=i%5<1)=println(b|f ? "Fizz"^f*"Buzz"^b : i)).(1:100)
3 Likes

66 is possible:

1:100 .|>i->(f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i))
6 Likes