Andrew Clarke wrote:
> I had a go at implementing the FizzBuzz task. Although a trivial conversion
> that tests for and outputs three different strings (a popular solution across
> the languages) I was hoping to do something clever that could
>
> test for 3 and output Fizz
> test for 5 and output Buzz
> if either or both execute, naturally bypass printing the number
>
> but everything I tried was messy, nerdy or incorrect.
>
> procedure main()
> every i := 1 to 100 do
> write("" ~== (if i % 3 = 0 then "Fizz" else "") || (if i % 5 == 0
> then
> "Buzz" else "") | i)
> end
>
> This works, but smacks of excessive cleverness rather than good coding.
>
> Thoughts?
Not really. I came up with:
procedure main()
every i := 1 to 100 do {
s := if i%3 = 0 then "Fizz" else ""
s ||:= if i%5 = 0 then "Buzz"
write(("" ~= s) | i)
}
end
and
procedure main()
every i := 1 to 100 do {
write(case 0 of {
i%15: "FizzBuzz"
i%3: "Fizz"
i%5: "Buzz"
default: i
} )
}
end
and
procedure main()
every i := 1 to 100 do {
write((i%15=0, "FizzBuzz") | (i%3=0, "Fizz") | (i%5=0, "Buzz") | i)
}
end
But none are particularly appealing. I guess I have a slight preference
for the last.
--
Steve Wampler -- [email protected]
The gods that smiled on your birth are now laughing out loud.
------------------------------------------------------------------------------
Download Intel® Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
_______________________________________________
Unicon-group mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/unicon-group