Just realised there is no Julia soluton to this on Code golf code golf - 1, 2, Fizz, 4, Buzz - Code Golf Stack Exchange
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.
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);
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
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
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
.
You can do āi%3<1ā for 2 bytes.
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:
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
Not sure if people prefer Java or they just learnt it at uni or forced to use it at work.
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.
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)
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;;$/<>?{$/"./_
Same side length as M Lās solution, but a bit smaller.
answered Mar 24 '18 at 11:40
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):
[ā¦] Hexagony is not that hard to work with. But golfing it can be a pain in the butt.
It was fun!
answered Mar 5 '16 at 21:56
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)
66 is possible:
1:100 .|>i->(f=i%3<1;b=i%5<1;println(f|b ? "Fizz"^f*"Buzz"^b : i))