Mainly Tech projects on Python and Electronic Design Automation.

Saturday, June 21, 2008

More Fizz than Buzz

I
downloaded the first beta release of Python 3 which corresponded with
another href="http://groups.google.com/group/comp.lang.python/msg/e7acbdd108a0ea05">thread
on FizzBuzz in c.l.p that featured some slick maths using href="http://en.wikipedia.org/wiki/Fermats_little_theorem">Fermats
little theorem, (yep it's really called that). I learnt a
little about the theorem, did a quick spreadsheet to show that it
worked, but could not grasp how they made it work in their examples.

It
did however get me toying with Py3K where I came up with this snippet
of code from the description of the problem.

The
Problem:
face="Courier, Monospaced">Write a program that prints the
numbers from 1 to 100. But for
multiples of three print
"Fizz" instead of the number and for the
multiples of five
print "Buzz". For numbers which are multiples of
both three
and five print class="fixed_width"> " style="font-family: monospace;">FizzBuzz"
 
My
Py3K solution:
style="margin-left: 40px; font-family: monospace;">
print( ' color="#ff00ff"> '.join(
    ' color="#ff00ff">FizzBuzz' color="#804040">if i%15 == 0
     color="#804040">else ' color="#ff00ff">Buzz' if i%5 == 0
     color="#804040">else ' color="#ff00ff">Fizz' if i%3 == 0
     color="#804040">else str(i)
     color="#804040">for i color="#804040">in range(1,101)))

What
I like about this
solution is it applies the constraints from the problem description in
the (reverse) order that they are given, and does not rely on the
elementary (to pythoneers), knowledge that a test for not(X) is
equivalent to a test of whether X==0. I'd be happy to put this in, say
a Wikipedia type article on FizzBuzz, or teach it to newbies ready for
immersion into gencomps (as maybe the second or third
example). 

- One for trainers.

-
Paddy.

Followers

Subscribe Now: google

Add to Google Reader or Homepage

Go deh too!

whos.amung.us

Blog Archive