FizzBuzz() Out : Text[1024]
FOR i := 1 to 100 DO BEGIN
IF i MOD 3 = 0 THEN
out += ‘Fizz’;
IF i mod 5 = 0 THEN
out += ‘Buzz’;
If NOT (i mod 3 = 0) AND NOT (i mod 5 = 0) THEN
out += Format(i);
out += ‘’;
END;
A modern & practical approach … (written in Go in this case, but any language will do)
package main
import (
"fmt"
"github.com/someone-else-who-has-done-all-the-work-already/fizzbuzz"
)
func main () {
fmt.Println( fizzbuzz(1,100) )
}
Its not cheating, and it does address the original intention of the FizzBuzz problem.
I think this is a valid solution, because (for better or for worse), the ability to collaborate with total strangers and existing solutions has become an important tool in the programmer’s arsenal,
Its also idiomatic Go (GoLang), to #include directly into a 3rd party github repo, support for which is built into the language from day 1. The compiler manages the fetching, building, regression testing of the 3rd party libs, as they change over time.
Given that the spec for FizzBuzz may well change over time … each time you compile this code, it may update itself automatically, and produce a different result ???
This has some wide philosophical implications when you dwell on that.
You can easily achieve the same thing in any toolchain by writing some extra lines in your Makefile or whatever. Go just sticks it right in your face and makes it obvious.
Is this what the customer actually asked for ? Nope !
Is this what the customer actually meant ? Probably !
Will asking the customer to clarify what they meant yield a correct answer ? Not very likely !
And also in my very own programming language that I wrote in ruby (called Bike):
def fb (i) {
if i % 15 is 0 { println i }
if i % 3 is 0 { println "Fizz" }
if i % 5 is 0 { println "Buzz" }
}
def fizzbuzz (max) {
fb max
if max > 0 {
fizzbuzz max - 1
}
}
fizzbuzz 100
here is the language on github (It is still in Beta 4):
I haven’t seen an R language response yet. Admittedly, I didn’t read every one of them and it’s hard to ctrl-F for R and not get a zillion responses:
x <- seq(1, 100, 1)
fooish <- function(x){
y <- x
y[x %% 3 == 0] <- "Fizz"
y[x %% 5 == 0] <- "Buzz"
y[x %% 15 == 0] <- "FizzBuzz"
y <- as.data.frame(y)
return(y)
}
My first function was unwieldy (25 lines), took only a minute or so to write, but was successful. I went through another 15-line iteration, before finally paring it down to the 8 lines above.
Solution in Pascal. Took me more time but I am no professional just a beginner hobbyist.
var
i: integer;
Fizz, Buzz, Fizbuzz: Boolean;
begin
For i:=1 to 100 do
begin
if i mod 15 = 0 then Fizbuzz := True else Fizbuzz := False;
if i mod 3 = 0 then Fizz := True else Fizz := False;
if i mod 5 = 0 then Buzz := True else Buzz := False;
if Fizbuzz then Writeln(i, ' FIZZBUZZ')
else
begin
if Fizz then Writeln(i, ' FIZZ');
if Buzz then Writeln(i, ' BUZZ');
end;
if not Fizz and not Buzz then Writeln(i);
for i in {1…100}; do if [ $(($i % 3)) -eq 0 -a $(($i %5)) -eq 0 ]; then echo “FizzBuzz”; elif [ $(($i %5)) -eq 0 ]; then echo “Buzz”; elif [ $(($i % 3)) -eq 0 ]; then echo “Fizz”; else echo $i; fi; done
Or, to make it a bit more readable:
for i in {1..100}
do
if [ $(($i % 3)) -eq 0 -a $(($i %5)) -eq 0 ]
then
echo "FizzBuzz"
elif [ $(($i %3)) -eq 0 ]
then
echo "Fizz"
elif [ $(($i % 5)) -eq 0 ]
then
echo "Buzz"
else
echo $i
fi
done
OR - how about PL/SQL (Oracle’s bastard stepchild of Ada + embedded SQL)?
BEGIN
FOR i IN 1..100 LOOP
IF MOD(i, 3) = 0 AND MOD(i, 5) = 0 THEN
DBMS_OUTPUT.PUT_LINE('FizzBuzz');
ELSIF MOD(i, 3) = 0 THEN
DBMS_OUTPUT.PUT_LINE('Fizz');
ELSIF MOD(i, 5) = 0 THEN
DBMS_OUTPUT.PUT_LINE('Buzz');
ELSE
DBMS_OUTPUT.PUT_LINE(i);
END IF;
END LOOP;
END;
int i;
for (i=0; i<=100; i++){
if (i%3==0&&i%5!=0){ //the && i%5!=0 is so it does not enter here if its a FizzBuzz
System.out.println("Fizz");
}
if (i%5==0&&i%3!=0){ //the && i%3!=0 is so it does not enter here if its a FizzBuzz
System.out.println("Buzz");
}
if (i%5==0&&i%3==0){ //Two conditions apply so it gives FizzBuzz
System.out.println("FizzBuzz");
}
if (i%5!=0&&i%3!=0){ //For Everything Else
System.out.println(i);
}
}
}