Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.forth > #16050

Re: Fizz Buzz Zoom

From Paul Rubin <no.email@nospam.invalid>
Newsgroups comp.lang.forth
Subject Re: Fizz Buzz Zoom
References <7xhaq645m0.fsf@ruckus.brouhaha.com> <2012Oct8.160229@mips.complang.tuwien.ac.at>
Date 2012-10-08 08:38 -0700
Message-ID <7xpq4tdldn.fsf@ruckus.brouhaha.com> (permalink)
Organization Nightsong/Fort GNOX

Show all headers | View raw


anton@mips.complang.tuwien.ac.at (Anton Ertl) writes:
> What is the specification?  What is the Python version?

It's a version of the famous FizzBuzz interview question:

  http://www.codinghorror.com/blog/2007/02/why-cant-programmers-program.html

The original question only used "Fizz" and "Buzz" and the most common
solution involves dealing with the case n%15==0 separately from the
n%3==0 and n%5==0 cases, which I thought was ugly.  There is not a
canonical Python version.  There's a discussion here of a bunch of
Haskell and Python solutions that I mostly didn't like, because of too
much abstraction (the Haskell monoid versions), or not enough (the
Python version everyone liked handles the empty string incorrectly):

  http://www.reddit.com/r/haskell/comments/10zlyy/fizzbuzz_revisited_using_monoids/ 

FizzBuzz also shows up in the coding game Code Hero:

  http://primerlabs.com/fizzbosses

My preferred version separates the code from the data ("figure out the
data structure, and the code takes care of itself").  First write down
the data:

     spec = [(3, "Fizz"), (5, "Buzz"), (7, "Zoom")]

The following code then flows pretty easily (be aware that ''.join(...)
is a Python idiom for concatenating a list of strings, and that a
boolean test on a list returns true iff the list is non-empty):

     def fb(n):
        ns = [s for (d,s) in spec if n%d==0]
        return ''.join(ns) if ns else str(n)

     for i in xrange(1,121): print fb(i)

This handles arbitrary numbers of divisors and does the right thing if
one or more of the strings is empty.  In Haskell I'd probably code it
the right way, or if I did it with monoids I'd use mconcat instead of
that thing with monad comprehensions (don't worry if that makes no
sense).

According to the Wikipedia article about FizzBuzz which I just read last
night, it was originally a drinking game rather than a programming
challenge, and the syllable for divisor 7 is traditionally "Woof" rather
than "Zoom".  One learns all kinds of things on the internets.

Back to comp.lang.forth | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-07 09:19 -0700
  Re: Fizz Buzz Zoom mhx@iae.nl (Marcel Hendrix) - 2012-10-07 20:33 +0200
    Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-07 11:43 -0700
      Re: Fizz Buzz Zoom mhx@iae.nl (Marcel Hendrix) - 2012-10-07 21:09 +0200
        Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-07 12:18 -0700
          Re: Fizz Buzz Zoom ouatubi@gmail.com - 2012-10-08 01:15 -0700
            Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 09:37 -0700
              Re: Fizz Buzz Zoom ouatubi@gmail.com - 2012-10-09 01:16 -0700
  Re: Fizz Buzz Zoom Doug Hoffman <glidedog@gmail.com> - 2012-10-07 14:45 -0400
    Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-07 12:28 -0700
      Re: Fizz Buzz Zoom Doug Hoffman <glidedog@gmail.com> - 2012-10-07 21:06 -0400
        Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-07 21:32 -0700
          Re: Fizz Buzz Zoom Doug Hoffman <glidedog@gmail.com> - 2012-10-08 06:06 -0400
            Re: Fizz Buzz Zoom Mark Wills <forthfreak@gmail.com> - 2012-10-08 05:24 -0700
  Re: Fizz Buzz Zoom anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-10-08 14:02 +0000
    Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 08:38 -0700
      Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 08:43 -0700
      Re: Fizz Buzz Zoom "A. K." <akk@nospam.org> - 2012-10-08 18:03 +0200
      Re: Fizz Buzz Zoom anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-10-08 16:08 +0000
        Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 09:23 -0700
          Re: Fizz Buzz Zoom anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-10-08 17:11 +0000
            Re: Fizz Buzz Zoom Bernd Paysan <bernd.paysan@gmx.de> - 2012-10-08 22:12 +0200
            Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 13:16 -0700
              Re: Fizz Buzz Zoom Hannu Vuolasaho <hannu.vuolasaho@nospam.tut.fi.invalid> - 2012-10-08 21:06 +0000
                Re: Fizz Buzz Zoom "Elizabeth D. Rather" <erather@forth.com> - 2012-10-08 11:57 -1000
                Re: Fizz Buzz Zoom Mark Wills <forthfreak@gmail.com> - 2012-10-09 01:26 -0700
                Re: Fizz Buzz Zoom George Hubert <georgeahubert@yahoo.co.uk> - 2012-10-09 08:38 -0700
                Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-09 09:03 -0700
                Re: Fizz Buzz Zoom "Elizabeth D. Rather" <erather@forth.com> - 2012-10-09 08:44 -1000
              Re: Fizz Buzz Zoom anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-10-10 12:32 +0000
                Re: Fizz Buzz Zoom Bernd Paysan <bernd.paysan@gmx.de> - 2012-10-10 16:59 +0200
                Re: Fizz Buzz Zoom anton@mips.complang.tuwien.ac.at (Anton Ertl) - 2012-10-10 16:42 +0000
                Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-12 21:18 -0700
                Re: Fizz Buzz Zoom "Elizabeth D. Rather" <erather@forth.com> - 2012-10-12 21:06 -1000
      Re: Fizz Buzz Zoom Doug Hoffman <glidedog@gmail.com> - 2012-10-10 09:02 -0400
  Re: Fizz Buzz Zoom Gerry Jackson <gerry@jackson9000.fsnet.co.uk> - 2012-10-08 17:50 +0100
    Re: Fizz Buzz Zoom Paul Rubin <no.email@nospam.invalid> - 2012-10-08 10:15 -0700

csiph-web