Monday, March 19, 2007

FizzBuzz

Here's a quick XQuery solution to the FizzBuzz problem posed in David Patterson's blog.

I've taken the liberty of splitting the hyphenated string into two attributes.

let $config :=
<fizzbuzz>
<range min="1" max="100"/>
<test>
<mod value="3" test="0">Fizz</mod>
<mod value="5" test="0">Buzz</mod>
</test>
</fizzbuzz>

for $i in ($config/range/@min to $config/range/@max)
let $s := for $mod in $config/test/mod
return
if ($i mod $mod/@value = $mod/@test)
then string($mod)
else ()
return
if (exists($s))
then string-join($s,' ')
else $i


3 comments:

M. David Peterson said...

Hi Chris,

re: [Not sure how to retain the indentation in this blog]

You *should* be able to use,

<pre>
<code>

... code ...

</code>
</pre>

which *should* take care of it (I add the emphasis on should, as I have no idea how blogspot handles additional inline markup)

re: your solution. This is great! Will add this to the Dev.AOL solution collection entry now.

Thanks!

chris wallace said...

Many thanks David - fixed.

M. David Peterson said...

Right on. Good to know that it works :)

For reference, I've added id's to each entries beginning paragraph. Your entry is @ http://dev.aol.com/blog/mdavidpeterson/2007/03/14/fizz-buzz-in-xslt-2.0#5