FizzBuzz Solution Dumping Ground


#61
  1. After f in hex comes 10.

  2. Swap two variables (regardless of type)

list($b, $a) = array($a, $b);

  1. Fizzbuzz in PHP:

foreach (range(1,100) as $i) {
if (!($i % 3)) $o = ‘Fizz’;
if (!($i % 5)) $o .= ‘Buzz’;
if (!$o) $o = $i;
echo $o . “\n”;
unset($o);
}

Do I get the job? :wink:


#62

Yes this is sad, but I had to type it up…

class Program
{
private const string Fizz = “Fizz”;
private const string Buzz = “Buzz”;

	static void Main(string[] args)
	{
		for (int i = 1; i = 100; i++)
		{
			bool bMultipleOf3 = (i % 3 == 0);
			bool bMultipleOf5 = (i % 5 == 0);

			if (bMultipleOf3)
			{
				Console.Write(Fizz);
			}
			if (bMultipleOf5)
			{
				Console.Write(Buzz);
			}

			if (!(bMultipleOf3 || bMultipleOf5))
			{
				Console.Write(i);
			}

			Console.WriteLine();
		}
		Console.ReadLine();
	}
}

#63

Here is my code to produce my solution:

#include stdio.h
int main(void)
{
printf("#include stdio.h\nint main(void)\n{\n");
int i;
for (i = 1; i = 100; i++)
{
printf("\tprintf("");
if (i % 3 == 0 i % 5 == 0)
printf(“FizzBuzz”);
else if (i % 3 == 0)
printf(“Fizz”);
else if (i % 5 == 0)
printf(“Buzz”);
else
printf("%d", i);
printf("");\n");
}
printf("\treturn 0;\n}\n");
return 0;
}


#64

To have something extravagant …

#_SET:A=[1…100]#
#_FOREACH:A=LINE#
#_BEGIN_DELETE_IFNOT:LINE%3==0#FIZZ#_END_DELETE_IFNOT##_BEGIN_DELETE_IFNOT:LINE%5==0#BUZZ#_END_DELETE_IFNOT##_BEGIN_DELETE_IF:LINE%3==0||LINE%5==0##LINE##_END_DELETE_IF#
#_ENDFOREACH#

:slight_smile:


#65

Sorry, syntax error … should be:
#_BEGIN_FOREACH … _END_FOREACH#


#66

I like mine in php… you can set the index and how many you want to do.

?php
//FizzBuzz
Function FizzBuzz($index, $num) {
for($i = $index; $i=$num; $i++) {
if($i%3==0 $i%5==0) {
echo “FizzBuzzbr /”;
}
else {
if($i%3==0) {
echo “Fizzbr /”;
}
elseif($i%5==0) {
echo “Buzzbr /”;
}
else {
echo “$ibr /”;
}
}
}
}

FizzBuzz(1,100);

?


#67

Print “1”
Print “2”
Print “Fizz”
Print “4”
Print “Buzz”
Print “Fizz”
Print “7”
Print "8"
Print "Fizz"
Print "10"
Print "Doh!"
Print "Fizz"
Print "13"
Print "14"
Print “FizzBuzz” – which most peeps above forgot

and so on.
I can’t see what the fuss is…


#68

Very easy in C#:

private void FizzBuzz() {
string fizz;
string buzz;

for (int i = 1; i = 100; i++) {
    fizz = null; buzz = null;

    if (i % 3 == 0) {
        fizz = "Fizz";
    }
    if (i % 5 == 0) {
        buzz = "Buzz";
    }
    if (fizz != null || buzz != null) {
        Console.WriteLine(fizz + buzz);
    }
    else {
        Console.WriteLine(i.ToString());
    }
}

}


#69

Surely this is a test about reading the question (i.e. outputting the numbers)
and efficiency, minimising the number of conditional tests.

So the mod3 mod5 elegant solution misses the non replaced numbers and does 2 conditional checks per loop

This is better:

while(i++ i=100)
{
if(i%3)//most likely so first
write(“Fizz”)
if((i%5))//next likely
write(“Fizz”)
else if (i%5))//next likely
write(“Fizz”)
else
write(i)
}

even with this for a non replaced number all 3 conditions are checked first


#70

Writing a program that satisfies your FizzBuzz specification is trivial in any language. The solve the specification you can simply write out the precomputed solution:

println(“1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz …”);

This gives you also the best runtime performance …


#71

//define your own output method with TRACE
void fizzbuzz(int i = 1)
{
if (i 100) return;
(i%15)?(i%3)?(i%5)?TRACE("%d",i):TRACE(“Buzz”):TRACE(“Fizz”):TRACE(“FizzBuzz”);
TRACE("\n");
fizzbuzz(++i);
}
void main()
{
fizzbuzz();
}
//yep, I know this kinf of code is a nightmare for thos who will mantain this code later :wink:


#72

Dim i As Integer = 1
Dim newline = False

    For i = 1 To 100
        Response.Write("br/")
        If (i Mod 3 = 0) Then
            Response.Write("Fizz")
        End If
        
        If (i Mod 5 = 0) Then
            Response.Write("Buzz")
        ElseIf (i Mod 3  0 And i Mod 5  0) Then
            Response.Write(i)
        End If            
    Next
End Sub

#73

for (int i=0;i101 ;i++)
{
System.out.println((i%3==0)?((i%5==0)?“fizzbuzz”:“fizz”):(i%5==0)?“buzz”:i+"");
}

programmers are idiots.


#74

PHP:

while(++$i101) {
if(!($i%15)) echo “FizzBuzz\n”;
else if(!($i%3)) echo “Fizz\n”;
else if(!($i%5)) echo “Buzz\n”;
else echo “$i\n”
}


#75

Welcome to school :slight_smile:

For the VBScripters that were arguing over carriage returns, a CSscript solution in about 30 seconds:
Dim i
For i = 1 to 100
If i mod 3 = 0 Then WScript.StdOut.Write "Fizz"
If i mod 5 = 0 Then WScript.StdOut.Write "Buzz"
If CBool(i mod 3) And CBool(i Mod 5) Then WScript.StdOut.Write i
WScript.Echo ""
Next

Next we use VBScript to do it in ASP (ok, this is cheating :stuck_out_tongue: ):
%
Option Explicit

Dim i
For i = 1 to 100
If i mod 3 = 0 Then Response.Write "Fizz"
If i mod 5 = 0 Then Response.Write "Buzz"
If CBool(i mod 3) And CBool(i Mod 5) Then Response.Write i
Response.Write "br/"
Next
%

Then something else…python?
a = []
for i in range(1,101):
a.append((i%15==0) and “FizzBuzz” or ((i%5==0) and “Buzz” or ((i%3==0) and “Fizz” or str(i))))
print a

Damn, time to take a shower and go to work. I think I just wasted most of my morning reading the other replies before I spent a couple minutes writing those three :stuck_out_tongue:


#76

Another C# solution:

for (int i = 1; i = 100; i++) {
if (i % 3 == 0 || i % 5 == 0) {
if (i % 3 == 0) {
Console.Write(“Fizz”);
}
if (i % 5 == 0) {
Console.Write(“Buzz”);
}
}
else {
Console.Write(i.ToString());
}
Console.Write("\n");
}


#77

i’m sick of seeing C/C++ and other {} enclosed solutions so here’s my VB6 version. spent less than 5 minutes on it, which is terrible going by other’s standards.

dim num as byte, printstr as string
for num=1 to 100
printstr=""
if num mod 3 = 0 then printstr=printstr “Fizz"
if num mod 5 = 0 then printstr=printstr “Buzz"
if printstr”” then
debug.print printstr
else
debug.print num
end if
next

btw, i’m not proving myself. i’m 18, and i’ve already tried making a 2d game in VB6. oddly enough, it failed.

all this degrading of other applicants has made me want to apply for a programming job now! still at school for 3 months though.
God, i’m so arrogant.


#78

Just for the sake of perversity, here’s a solution written as a Windows .BAT file:

@echo off

for /L %%I in (1,1,100) do call :FizzBuzz %%I
goto :EOF

:FizzBuzz
set I=%1
set /a T3=I %% 3
set /a T5=I %% 5
set /a T35=T3 + T5

if not %T35%==0 goto FizzBuzz5
echo FizzBuzz
goto :EOF

:FizzBuzz5
if not %T5%==0 goto FizzBuzz3
echo Buzz
goto :EOF

:FizzBuzz3
if not %T3%==0 goto FizzBuzzEcho
echo Fizz
goto :EOF

:FizzBuzzEcho
echo %I%
goto :EOF


#79

In action Script ^^

for (var a:Number = 0; a100; a++) {
if (a%15 == 0) {
trace(“Fizz-Buzz”);
} else if (a%5 == 0) {
trace(“Buzz”);
} else if (a%3 == 0) {
trace(“Fizz”);
} else {
trace(a);
}
}

or

for (var a:Number = 0; a100; a++) {
trace(a%15 == 0 ? “Fizz-Buzz” : a%5 == 0 ? “Buzz” : a%3 == 0 ? “Fizz” : a);
}

nice lol …


#80

It’s so BASIC:

for i = 1 to 100
a$ = ""
IF i mod 3 = 0 THEN a$ = a$+“Fizz"
IF i mod 5 = 0 THEN a$ = a$+“Buzz"
IF a$=”” THEN print i ELSE print a$
next i

30 seconds (but no PhD)