Discuss Scratch

TheUltimatum
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

very long and inefficient
_=[print(("fizzbuzz","fizz","buzz",i)[next((j for j,k in enumerate(map(lambda x:i%x<1,(15,3,5)))if k),-1)]) for i in range(1,101)]
LastContinue
Scratcher
500+ posts

The shortest FizzBuzz YOU can make!

Bringing this thing back to life (Necromancy)

for i = 0 to 100 do
  let fizz = min 1 (i mod 3)
  in
  let buzz = min 1 (i mod 5) * 10 in
  match fizz + buzz with
  0 -> print_endline "FizzBuzz"
  | 10 -> print_endline "Fizz"
  | 1 -> print_endline "Buzz"
  | 11 -> print_endline (string_of_int i)
  ;
  done
  ;;

Also I forgot to do the OCaml for 2 months so it's really bad

Last edited by LastContinue (Oct. 9, 2019 19:15:26)

rdococ
Scratcher
100+ posts

The shortest FizzBuzz YOU can make!

Lua, in 92 characters.

for i=1,100 do print(i%15==0 and"FizzBuzz"or (i%3==0 and"Fizz"or (i%5==0 and"Buzz"or i)))end
ylem303
New to Scratch
1 post

The shortest FizzBuzz YOU can make!

C++
#include <cstdio>
#define S printf(
int main(){for(int i=1;i<101;i++){!(i%3)?S"Fizz"):0;!(i%5)?S"Buzz"):0;i%3&&i%5?S"%d",i):0;S"\n");}}

135 chars

Last edited by ylem303 (Nov. 22, 2019 07:20:55)

Wettining
Scratcher
500+ posts

The shortest FizzBuzz YOU can make!

ylem303 wrote:

C++
#include <cstdio>
#define S printf(
int main(){for(int i=1;i<101;i++){!(i%3)?S"Fizz"):0;!(i%5)?S"Buzz"):0;i%3&&i%5?S"%d",i):0;S"\n");}}

135 chars
You can make it shorter
TheAspiringHacker
Scratcher
100+ posts

The shortest FizzBuzz YOU can make!

LastContinue wrote:

Bringing this thing back to life (Necromancy)

for i = 0 to 100 do
  let fizz = min 1 (i mod 3)
  in
  let buzz = min 1 (i mod 5) * 10 in
  match fizz + buzz with
  0 -> print_endline "FizzBuzz"
  | 10 -> print_endline "Fizz"
  | 1 -> print_endline "Buzz"
  | 11 -> print_endline (string_of_int i)
  ;
  done
  ;;

Also I forgot to do the OCaml for 2 months so it's really bad
You don't need to do the hackery with the storing of the mod results as a two-digit number. You can do:
for i = 0 to 100 do
  match i mod 3, i mod 5 with
  | 0, 0 -> print_endline "FizzBuzz"
  | 0, _ -> print_endline "Fizz"
  | _, 0 -> print_endline "Buzz"
  | _, _ -> print_endline (string_of_int i)
done;;
If e1 and e2 are expressions, then the expression e1, e2 constructs a pair consisting of the result of e1 and the result of e2, and you can then pattern match on it. Putting a tuple as the match scrutinee is a common OCaml idiom for matching multiple values at the same time.

Aside from the general hackiness of the digit solution, this version is better because the pattern match is statically known to handle all cases. Although in the context of your code, 0, 10, 1, and 11 are the only possible values that can appear, those aren't the only values that an int can hold. In the new version, the combination of all patterns covers every possible value. An OCaml motto is “make illegal states unrepresentable,” and idiomatic OCaml takes advantage of the expressive type system, as opposed to compressing data into an int when the int type doesn't reflect the data's structure.

Here is a version that puts the print_endline outside the match cases:
for i = 0 to 100 do
  print_endline (
    match i mod 3, i mod 5 with
    | 0, 0 -> "FizzBuzz"
    | 0, _ -> "Fizz"
    | _, 0 -> "Buzz"
    | _, _ -> string_of_int i
  )
done;;
However, this version could be criticized for being too “clever.”

Last edited by TheAspiringHacker (Nov. 22, 2019 22:23:15)


Long live Kyoto Animation!
Wettining
Scratcher
500+ posts

The shortest FizzBuzz YOU can make!

TheAspiringHacker wrote:

for i = 0 to 100 do
  print_endline (
    match i mod 3, i mod 5 with
    | 0, 0 -> "FizzBuzz"
    | 0, _ -> "Fizz"
    | _, 0 -> "Buzz"
    | _, _ -> string_of_int i
  )
done;;
That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)

Last edited by Wettining (Nov. 24, 2019 15:51:59)

LastContinue
Scratcher
500+ posts

The shortest FizzBuzz YOU can make!

Wettining wrote:

TheAspiringHacker wrote:

for i = 0 to 100 do
  print_endline (
    match i mod 3, i mod 5 with
    | 0, 0 -> "FizzBuzz"
    | 0, _ -> "Fizz"
    | _, 0 -> "Buzz"
    | _, _ -> string_of_int i
  )
done;;
That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)
Python with Tuples maybe?
TheAspiringHacker
Scratcher
100+ posts

The shortest FizzBuzz YOU can make!

Wettining wrote:

TheAspiringHacker wrote:

for i = 0 to 100 do
  print_endline (
    match i mod 3, i mod 5 with
    | 0, 0 -> "FizzBuzz"
    | 0, _ -> "Fizz"
    | _, 0 -> "Buzz"
    | _, _ -> string_of_int i
  )
done;;
That's a really cool language feature of OCaml, do you know if any other languages have that? (The match case with the multi-input case)
The match case thing is called pattern matching, and the multi-input is just pattern matching on a tuple. Pattern matching is extremely common in typed functional languages.

According to Wikipedia, pattern matching originated in the NPL and Hope programming languages. Now, pattern matching has become a hallmark of the ML family, which OCaml, F#, and Standard ML are part of. Pattern matching is also a feature of Haskell (which shares some features with the ML family, but isn't really an ML), Swift, Rust, and Haxe.

The dependently typed languages Coq, Agda, and Idris have a more powerful feature called dependent pattern matching, in which the types of later patterns and the result may be refined by earlier matches.

Last edited by TheAspiringHacker (Nov. 25, 2019 04:36:20)


Long live Kyoto Animation!
ScratchCatHELLO
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

bump

Python 3.9.5, 65 chars, and 65 bytes??
for a in range(100):print('Fizz'*(a%3//2)+'Buzz'*(a%5//4) or a+1)

breakdown:

for a in range(1, 100):  #numbers from 1 to 100
print() #duh
'Fizz'*(a%3//2) #weird hack that adds one "Fizz" if a is divisible by three
+'Buzz'*(a%5//4) #ditto
or a+1 #my new favourite trick. if the rest of the string is empty (which happens when the number is not divisible by 3 or 5), the string is interpreted as False, which causes the string to be equal to a+1, because a+1 is True (I think)

demo of that last trick:
>>> a = 5
>>> 'abc'*(int(a==5))or'xyz'
'abc'
>>> a += 1
>>> 'abc'*(int(a==5))or'xyz'
'xyz'












enter command
West of House
This is an open field west of a white house, with a boarded front door.
There is a small mailbox here.
A rubber mat saying 'Welcome to Zork!' lies by the door.

>examine mailbox
I see nothing special about the mailbox.

>
















Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y



ScratchCatHELLO
I have 4300+ posts, I've been on scratch for 3 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust

Greg8128
Scratcher
500+ posts

The shortest FizzBuzz YOU can make!

Here's my Haskell version:
module Main where
import Control.Monad
main=forM_[1..100]$ putStrLn. q;q n|mod n 15<1="fizzbuzz"|mod n 3<1="fizz"|mod n 5<1="buzz"|1>0=show n
The first part is the loop while the second part declares the helper function “q”.
I included all the boilerplate for a compiled program (everything before “forM_” is arguably boilerplate) so that you can choose whether to count it towards the total length

Last edited by Greg8128 (June 30, 2021 01:53:45)


My best projects:

PkmnQ
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

ScratchCatHELLO wrote:

bump

Python 3.9.5, 65 chars, and 65 bytes??
for a in range(100):print('Fizz'*(a%3//2)+'Buzz'*(a%5//4) or a+1)

breakdown:

for a in range(1, 100):  #numbers from 1 to 100
print() #duh
'Fizz'*(a%3//2) #weird hack that adds one "Fizz" if a is divisible by three
+'Buzz'*(a%5//4) #ditto
or a+1 #my new favourite trick. if the rest of the string is empty (which happens when the number is not divisible by 3 or 5), the string is interpreted as False, which causes the string to be equal to a+1, because a+1 is True (I think)

demo of that last trick:
>>> a = 5
>>> 'abc'*(int(a==5))or'xyz'
'abc'
>>> a += 1
>>> 'abc'*(int(a==5))or'xyz'
'xyz'
You can shave off one byte by removing the space before the or. Also, Pyth.
Pyth, 34 chars/bytes
V100|+*"Fizz"/%N3 2*"Buzz"/%N5 4hN
It's basically the same as the Python solution, but in Pyth. Of course, it isn't very interesting, so I'm actually working on a more interesting one in a different programming language.

SEVERE GLITCH ALERT!
There's a bug called the question mark glitch. Basically, sometimes, some of your costumes turn into question marks in boxes. If you're looking for the glitch in one of your projects, do NOT press see inside. If it happens that the question marks are there, they will become permanent. Go here for more information. If you spot it while editing, close the tab without saving.

Here, have a useful link:
The Official List of Rejected Suggestions by Za-Chary

Why it's bad to run arbitrary javascript

Also, this is where I keep my pet kumquat. I put it here because after 59.907 seconds of research I have found out that kumquats aren't cannibals.
Cabbage_2020
Scratcher
82 posts

The shortest FizzBuzz YOU can make!

Just create a programming language
yay, 0 characters

yes i know it is 2021
(this is my second account, i am not new do the discussion forums)
()_()




meow?

open a new tab

{
}
e
e
e
e
e
bye, you can now go away.
mybearworld
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

what is a fizzbuzz? xD

2+2*5 = ?
2+2*5 = 20
2+2*5 = 12
2+2*5 = 77034585053198940266692661
gosoccerboy5
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

mybearworld wrote:

what is a fizzbuzz? xD
It's a basic coding interview question, where you iterate through the numbers 1-100
if the current number is divisible by 3 it prints “fizz”, if its divisible by 5 it prints “buzz” and if both it prints “fizzbuzz” (I think). otherwise it prints the number
for (let i = 1; i <= 100; i++) {
    if (i % 3 === 0) {
        if (i % 5 === 0) {
             console.log("FizzBuzz");
             continue;
        } else console.log("Fizz");
    } else if (i % 5 === 0) {
        console.log("Buzz");
    } else console.log(i);
}
(Yeah not the most efficient but ok)

Last edited by gosoccerboy5 (July 2, 2021 13:49:27)


Kerbal Space Program!!!
ScratchCatHELLO
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

Cabbage_2020 wrote:

Just create a programming language
yay, 0 characters

actually, if you build an entire programming language to do this, you should count the number of characters in the program that compiles/interprets the language.

mybearworld wrote:

what is a fizzbuzz? xD

you take the numbers from 1 to 100. if the number is divisible by three, print “Fizz”. if the number is divisible by five, print “Buzz”. if the number is divisible by three and five, print “FizzBuzz”. if the number is neither divisible by three or five, print the number.












enter command
West of House
This is an open field west of a white house, with a boarded front door.
There is a small mailbox here.
A rubber mat saying 'Welcome to Zork!' lies by the door.

>examine mailbox
I see nothing special about the mailbox.

>
















Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y



ScratchCatHELLO
I have 4300+ posts, I've been on scratch for 3 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust

gosoccerboy5
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

Javascript, 60 bytes (if `alert` is allowed)
for(i=1;i<=100;i++)alert((i%3?"":"Fizz")+(i%5?"":"Buzz")||i)

explanation:
for (i=1; i<=100; i++) // well duh we have to loop
  alert( // well duh we have to print the results
    (i % 3 ? "" : "Fizz") // if i%3 is not 0, it's truthy, so it prints "" but if it's 0 it will be falsy and print "fizz"
    + (i % 5 ? "" : "Buzz") // same principle, we also concatenate this to the previous one
    || i // if the last value was equal to "", then it's falsy, so we print the number instead
  ) // end of program
My fizzbuzz uses a lot of hacks.
- type coercion to boolean
- not declaring a variable before assigning it
- not using semicolons
- not using curly brackets on the for loop
(I guess the last two aren't too hacky)

I'm keeping this thing for job interviews in the future lol
(assuming I even get job interviews )

Last edited by gosoccerboy5 (July 1, 2021 17:21:05)


Kerbal Space Program!!!
gosoccerboy5
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

Za-Chary wrote:

Since it's Java, you can condense the entire code onto one line if you want to:

public class FizzBuzz {public static void main(String[] args) {for (int i = 1; i < 101; i++) {System.out.println(i % 3 == 0 && i % 5 == 0 ? "FizzBuzz" : (i % 3 == 0 ? "Fizz" : (i % 5 == 0 ? "Buzz" : i))); }}}
you can remove whitespace between operators and assignments.
public class FizzBuzz{public static void main(String[]args){for(int i=1;i<101;i++){System.out.println(i%3==0&&i%5==0?"FizzBuzz":(i%3==0?"Fizz":(i%5==0?"Buzz":i)));}}}
I think that should work
(Minified java never feels very minified because of all the whitespace between the keywords!)

Last edited by gosoccerboy5 (July 1, 2021 16:29:45)


Kerbal Space Program!!!
ScratchCatHELLO
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

python would be shorter if it used different syntax (for _ in _ vs for(){}), but weirdly the range function that python uses is actually shorter than the javascript equivalent because two of the arguments are optional and you don’t need to use i












enter command
West of House
This is an open field west of a white house, with a boarded front door.
There is a small mailbox here.
A rubber mat saying 'Welcome to Zork!' lies by the door.

>examine mailbox
I see nothing special about the mailbox.

>
















Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y



ScratchCatHELLO
I have 4300+ posts, I've been on scratch for 3 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust

ScratchCatHELLO
Scratcher
1000+ posts

The shortest FizzBuzz YOU can make!

gosoccerboy5 wrote:

Javascript, 60 bytes (if `alert` is allowed)
for(i=1;i<=100;i++)alert((i%3?"":"Fizz")+(i%5?"":"Buzz")||i)

explanation:
for(i=1; i<=100; i++) // well duh we have to loop
alert( // well duh we have to print the results
(i % 3 ? "" : "Fizz") // if i%3 is not 0, it's truthy, so it prints "" but if it's 0 it will be falsy and print "fizz"
+ (i % 5 ? "" : "Buzz") // same principle, we also concatenate this to the previous one
|| i // if the last value was equal to "", then it's falsy, so we print the number instead
) // end of program
I'm keeping this thing for job interviews in the future lol
(assuming I even get job interviews )

the biggest advantage that js has here over python is the ternary operator:
#python’s ternary operator is really easy to understand at a glance, at the cost of length
aif b==c elseb
//JS’ is much shorter, but confusing if you don’t know the syntax
a==b?b:a
it also has the smaller advantage of being able to put variables next to the or operator:
…||i
this is more than enough to cancel out the range() disadvantage












enter command
West of House
This is an open field west of a white house, with a boarded front door.
There is a small mailbox here.
A rubber mat saying 'Welcome to Zork!' lies by the door.

>examine mailbox
I see nothing special about the mailbox.

>
















Python 3 Text Adventure
cool new browser game - cursed laughing-crying emoji - Illuminati - you know waterbenders, but do you know stock-imagebenders? - snek - vibin' - Bump song (vevo) - Speed bump - yee - fred - m i c k e y



ScratchCatHELLO
I have 4300+ posts, I've been on scratch for 3 years, I'm a Forum Helper™ and I have a Scratch Wiki account!
I like: Python, CSS, Javascript, Rust

Powered by DjangoBB

Standard | Mobile