Win a copy of Five Lines of Code this week in the OO, Patterns, UML and Refactoring forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Fizz Buzz

 
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fizz Buzz Test: 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 “FizzBuzz”.

I was actually asked this on an interview, and wrote it in SQL or VB (don't remember). It's so hard to think straight on an interview. Pick a language, write the code. It's fun. No reason to be tricky, just do it. Here's an untested (temporarily) Oracle solution:


It uses the connect by trick for the count, and REMAINDER() is Oracle 10 or above. Probably could just use % (mod). Regardless, it's a straight forward answer. It's also fun to Code_golf with it, but try a simple answer first. Indeed, what do you consider simple?
 
Brian Tkatch
Bartender
Posts: 598
26
Oracle Notepad Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, that's the right logic, but wrong implementation.


Fixes:

  • REMAINDER needs a comma, not a solidus. (That's should have been obvious!)
  • The number must be CASTed so the entire CASE has the same data type.
  • CASE short circuits, so the 3-and-5 case must come first.
  •  
    Bartender
    Posts: 10777
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Brian Tkatch wrote:It uses the connect by trick for the count, and REMAINDER() is Oracle 10 or above. Probably could just use % (mod). Regardless, it's a straight forward answer. It's also fun to Code_golf with it, but try a simple answer first. Indeed, what do you consider simple?


    Well, my main criticism would be that your REMAINDER() functions are repeated, so the majority (8/15ths) of the time, your code executes 4 checks only to discover that the number doesn't divide by either multiplier.

    It also isn't very "scaleable". I wouldn't want to work out all the combos for "Fizz Buzz Pop Whack" for example.

    If, OTOH, you run your checks as if you were calculating a prime, and store the results in an array (or in Java, perhaps a BitSet), you could implement a "two-phase" solution that runs in close to optimal time.

    Obviously, not good for "golf" though....

    Fun question.

    Winston
     
    Brian Tkatch
    Bartender
    Posts: 598
    26
    Oracle Notepad Linux
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Winston Gutkowski wrote:Well, my main criticism would be that your REMAINDER() functions are repeated, so the majority (8/15ths) of the time, your code executes 4 checks only to discover that the number doesn't divide by either multiplier.


    I was thinking about that. It could be worked out as a CTE, but i just went for clarity. Regardless, it's a deterministic function, so Oracle would just grab the number from the cache anyway, meaning it won't be run a second time.

    Winston Gutkowski wrote:It also isn't very "scaleable". I wouldn't want to work out all the combos for "Fizz Buzz Pop Whack"


    Another CTE would likely solve any such issues. Or... or...So many approaches, so much fun.
     
    Java Cowboy
    Posts: 16084
    88
    Android Scala IntelliJ IDE Spring Java
    • Likes 2
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Have a look at our long Fizz Buzz challenge topic for more funny and interesting solutions.
     
    Winston Gutkowski
    Bartender
    Posts: 10777
    71
    Hibernate Eclipse IDE Ubuntu
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Jesper de Jong wrote:Have a look at our long Fizz Buzz challenge topic for more funny and interesting solutions.


    This is definitely my fave from that lot. Simple, elegant and fast. Not wildly scaleable though.
    The only thing I changed from Mike Simmons' original was to put the switch statement in a separate method, which eliminates a lot of breaks, but the idea is all his.

    Winston
     
      Bookmark Topic Watch Topic
    • New Topic