Skip to content

Instantly share code, notes, and snippets.

@KiloSwiss
Last active November 4, 2022 19:45
Show Gist options
  • Save KiloSwiss/3fa28e71c284e4ea2087a9192a5f967e to your computer and use it in GitHub Desktop.
Save KiloSwiss/3fa28e71c284e4ea2087a9192a5f967e to your computer and use it in GitHub Desktop.
Various examples of solutions to the infamous FizzBuzz test.
/* Arma3 SQF syntax - FizzBuzz examples */
// Example 1a
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
if ( _string isEqualTo "" ) then { _string = str _i };
diag_log _string;
};
// Example 1b - Using the alternative syntax for if: https://community.bistudio.com/wiki/if
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
diag_log ( if ( count _string isEqualTo 0 ) then [ { str _i } , { _string } ] );
};
// Example 1c - Using the alternative syntax for select: https://community.bistudio.com/wiki/select
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
diag_log ( [ _string, str _i] select ( count _string isEqualTo 0 ) );
};
// Example 2a "while loop"
private _i = 0;
while { _i < 100 } do
{
_i = _i + 1;
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
if ( _string isEqualTo "" ) then { _string = str _i };
diag_log _string;
};
// Example 2b "while loop"
private _i = 1;
while { _i <= 100 } do
{
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
if ( _string isEqualTo "" ) then { _string = str _i };
diag_log _string;
_i = _i + 1;
};
// Example 3a "switch do" - We define the variable "_string" in the code block (do) for each switch case.
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
switch ( true ) do
{
case ( _i%3 isEqualTo 0 && { _i%5 isEqualTo 0 } ): { _string = "FizzBuzz" };
case ( _i%3 isEqualTo 0 ): { _string = "Fizz" };
case ( _i%5 isEqualTo 0 ): { _string = "Buzz" };
default { _string = str _i };
};
diag_log _string;
};
// Example 3b "switch do" - But we define the variable "_string" directly as the result that switch returns.
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = switch ( true ) do
{
case ( _i%3 isEqualTo 0 && { _i%5 isEqualTo 0 } ): { "FizzBuzz" };
case ( _i%3 isEqualTo 0 ): { "Fizz" };
case ( _i%5 isEqualTo 0 ): { "Buzz" };
default { str _i };
};
diag_log _string;
};
// Example 3c "switch do" - See the third example 1 and example 4 of https://community.bistudio.com/wiki/switch_do
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = switch ( _i%15 ) do
{
default { str _i };
case 0: { "FizzBuzz" };
case 3;
case 6;
case 9;
case 12: { "Fizz" };
case 5;
case 10: { "Buzz" };
};
diag_log _string;
};
// Example 4a "Unnecessary redefinition of the variable _string"
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
_string = if ( _i%3 isEqualTo 0 ) then { "Fizz" } else { _string };
_string = if ( _i%5 isEqualTo 0 ) then { _string + "Buzz" } else { _string };
_string = if ( _string isEqualTo "" ) then { str _i } else { _string };
diag_log _string;
};
// Example 4b - Using the alternative syntax for if: https://community.bistudio.com/wiki/if
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
_string = if ( _i%3 isEqualTo 0 ) then [ { "Fizz" }, { _string } ];
_string = if ( _i%5 isEqualTo 0 ) then [ { _string + "Buzz" }, { _string } ];
_string = if ( _string isEqualTo "" ) then [ { str _i }, { _string } ];
diag_log _string;
};
// Example 4c - Using the alternative syntax for select: https://community.bistudio.com/wiki/select
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
_string = [ _string, "Fizz" ] select ( _i%3 isEqualTo 0 );
_string = [ _string, _string + "Buzz" ] select ( _i%5 isEqualTo 0 );
_string = [ _string, str _i ] select ( _string isEqualTo "" );
diag_log _string;
};
// Example 5a - Working with predefined arrays.
private _fizzArray = [3,6,9,12,15,18,21,24,27,30,33,36,39,42,45,48,51,54,57,60,63,66,69,72,75,78,81,84,87,90,93,96,99];
private _buzzArray = [5,10,15,20,25,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100];
for "_i" from 1 to 100 /* step 1 */ do
{
if ( _i in _fizzArray ) then
{
if ( _i in _buzzArray ) then
{
diag_log "FizzBuzz";
}
else
{
diag_log "Fizz";
};
}
else
{
if ( _i in _buzzArray ) then
{
diag_log "Buzz";
}
else
{
diag_log str _i;
};
};
};
// Example 5b "Nested and convoluted if statements"
for "_i" from 1 to 100 /* step 1 */ do
{
if ( _i%3 isEqualTo 0 ) then
{
if ( _i%5 isEqualTo 0 ) then
{
diag_log "FizzBuzz";
}
else
{
diag_log "Fizz";
};
}
else
{
if ( _i%5 isEqualTo 0 ) then
{
diag_log "Buzz";
}
else
{
diag_log str _i;
};
};
};
// Example 5c "Nested and convoluted if statements"
for "_i" from 1 to 100 /* step 1 */ do
{
private _isFizz = _i%3 isEqualTo 0;
private _isBuzz = _i%5 isEqualTo 0;
if ( _isFizz && { _isBuzz } ) then
{
diag_log "FizzBuzz";
}
else
{
if ( _isFizz ) then
{
diag_log "Fizz";
}
else
{
if ( _isBuzz ) then
{
diag_log "Buzz";
}
else
{
diag_log str _i;
};
};
};
};
// Example 6a - Confusing. Has unnecessary use of if statements that each get checked multiple times, no matter what the previous results were.
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
if ( _i%3 == 0 && _i%5 != 0 ) then { _string = "Fizz" };
if ( _i%3 != 0 && _i%5 == 0 ) then { _string = "Buzz" };
if ( _i%3 == 0 && _i%5 == 0 ) then { _string = "FizzBuzz" };
if ( _i%3 != 0 && _i%5 != 0 ) then { _string = str _i };
diag_log _string;
};
// Example 6b - Confusing. Has unnecessary use of if statements that each get checked multiple times, no matter what the previous results were.
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = "";
private _isFizz = _i%3 isEqualTo 0;
private _isBuzz = _i%5 isEqualTo 0;
if ( _isFizz && !_isBuzz ) then { _string = "Fizz" };
if ( !_isFizz && _isBuzz ) then { _string = "Buzz" };
if ( _isFizz && _isBuzz ) then { _string = "FizzBuzz" };
if ( !_isFizz && !_isBuzz ) then { _string = str _i };
diag_log _string;
};
// Example 7 - Not recommended to use "waitUntil" this way.
private _i = 0;
waitUntil
{
_i = _i + 1;
private _string = "";
if ( _i%3 isEqualTo 0 ) then { _string = "Fizz" };
if ( _i%5 isEqualTo 0 ) then { _string = _string + "Buzz" };
if ( _string isEqualTo "" ) then { _string = str _i };
diag_log _string;
_i >= 100
};
// Example 8 - The showoff wannabe that writes unreadable code but it is shorter so he's cooler than the kid with the skateboard.
for "_i" from 1 to 100 /* step 1 */ do
{
private _string = (if(_i%3==0)then[{"Fizz"},{""}]) + (if(_i%5==0)then[{"Buzz"},{""}]);
diag_log [_string, str _i] select (count _string == 0);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment