Skip to main content

I like to give my team little challenges to exercise their NQE skills. Why don’t you play along this weekend and let’s see all the different ways you can accomplish this goal.

 

Tune in to see some creative ways to solve the FizzBuzz challenge with NQE

FizzBuzz


The task is to write a program that prints the numbers from 1 to 100, but with a twist:

  • For multiples of three, print "Fizz" instead of the number.
  • For multiples of five, print "Buzz" instead of the number.
  • For numbers which are multiples of both three and five, print "FizzBuzz" instead of the number.

Is the penalty to take a sip of your drink for  an incorrect Fizz/Buzz/FizzBuzz result?
This brings back memories.


Here’s the straightforward approach,

eval(i) = if i % 3 == 0 && i % 5 == 0 then "FizzBuzz" else
if i % 3 == 0 then "Fizz" else
if i % 5 == 0 then "Buzz"
else toString(i);

foreach i in fromTo(0, 100)
select {i: eval(i)}

Here’s a slightly more clever approach,


div3(i) = if i % 3 == 0 then "Fizz" else "";
div5(i) = if i % 5 == 0 then "Buzz" else "";
or(str,i) = if length(str) == 0 then toString(i) else str;

foreach i in fromTo(0, 100)
select {i: or(div3(i) + div5(i), i) }

This was fun, thanks! 😄


I think we need to exclude 0 in the result. Being pedantic….

foreach i in fromTo(1, 100)
let response = if i % 3 == 0 && i % 5 == 0
then "FizzBuzz"
else if i % 3 == 0
then "Fizz"
else if i % 5 == 0 then "Buzz" else toString(i)
select { i: response }

I do like the clever approach above though !


A silly solution as well….

timeX(x) =
foreach i in fromTo(1, 100)
where i % x == 0
select toString(i);

foreach x in n0]
let FizzBuzz = timeX(3 * 5)
let Fizz = timeX(3) - FizzBuzz
let Buzz = timeX(5) - FizzBuzz
let Numbers = timeX(1)
let Numeric = Numbers - Fizz - Buzz - FizzBuzz
foreach i in Numbers
let x = if i in Numeric
then i
else if i in Fizz
then "Fizz"
else if i in Buzz then "Buzz" else "FizzBuzz"
select { x }

 


Reply