# Let's have some fun - FizzBuzz in Flow

People here are coding FizzBuzz in various languages.

Pseudocode:

``````for every integer 1 to 100
if the integer is divisible by 3 and divisible by 5
print "FizzBuzz"
else if the integer is divisible by 3
print "Fizz"
else if the integer is divisible by 5
print "Buzz"
else
print the integer``````

‘Ruby golf’ version:

``100,{)..3%!'Fizz'*\5%!'Buzz'*+\or}%n*``

I managed it in 13 Flow steps - using nothing but built in services. See the screenshot there. I couldn’t find anything to give me modulus, so I had to use counters that reset to zero.

Anyone fancy trying to improve it?

You could reduce the number of steps by 3 by using a map step with 4 transformer calls to addInts. And I’m not sure you need the clearPipeline step. That would get it to 9 steps.

Or increase it by one, if you consider a transformer to be a step in itself (albeit in different part of the screen).

Since other solutions “extended” their environments, I created a “remainder” service.

service: remainder
inputs: num1 (string), num2 (string)
outputs: value (string)

IDataCursor idc = pipeline.getCursor();
int num1 = IDataUtil.getInt(idc, “num1”, 0);
int num2 = IDataUtil.getInt(idc, “num2”, 1); // avoid div by 0 exceptions
IDataUtil.put(idc, “value”, “”+num1%num2);
idc.destroy();

Then, using that remainder service in the FizzBuzz solution:

service: fizzBuzz
inputs: max (string)
outputs: outputs (string list)

``````REPEAT (count set to %/max%)
MAP (call remainder twice, passing \$retries as num1, and 3 & 5 as num2; map \$retries to s)
BRANCH (eval labels)
(%/rem3% == 0) && (%/rem5% == 0): MAP (map 'FizzBuzz' to s)
%/rem3% == 0: MAP (map 'Fizz' to s)
%/rem5% == 0: MAP (map 'Buzz' to s)
BRANCH on '/\$retries'
/[^ ]/: pub.list.appendToStringList (drop all vars but max, \$retries and output)``````

Eight steps. Could be 7 if it didn’t ignore 0.

You could drop the last two steps (append and clearPipeline) in favor of a single debugLog. Most of the other contributors just printed, rather than accumulating.

Only other thing that comes to mind is cheating on the divisible by 5 - divisibility rules say that all whole numbers divisible by 5 will end in 0 or 5, so you could regex your branch to look for /[05]\$/ - and then drop the increment addint services for buzz.

If we’re allowing outselves helpers, one could write a numToFizzBuzz service.

service: numToFizzBuzz
inputs: num (string)
outputs: value (string)

IDataCursor idc = pipeline.getCursor();
int num = IDataUtil.getInt(idc, “num1”, 0);
String value = Integer.toString(num);
num % 3 == 0 && value = “Fizz”;
num % 5 == 0 && value = “Buzz”;
num % 15 == 0 && value = “FizzBuzz”;
IDataUtil.put(idc, “value”, value);
idc.destroy();

The Flow that uses this can be very compact. But it would only be substituting for the simplest for() loop there can be.

Good point. That pretty much does the whole thing, except the 1…100 loop as you point out.

Rather than have Java do effectively all the work, I was trying to use FLOW for the things that it can do. The capability of FLOW is controlling the sequence of steps–making decisions. FLOW doesn’t have any operators at all, so we must resort to services for math, string, etc. manipulation. I added another “manipulator” not a decision maker.

Interesting exercise though, and interesting to see the different approaches.