DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
  1. DZone
  2. Coding
  3. Languages
  4. Fizzbuzz Without Ifs in 90 Characters -- I'll Buy You a Beer if You Can Beat It

Fizzbuzz Without Ifs in 90 Characters -- I'll Buy You a Beer if You Can Beat It

Swizec Teller user avatar by
Swizec Teller
·
Sep. 21, 12 · Interview
Like (1)
Save
Tweet
Share
10.39K Views

Join the DZone community and get the full member experience.

Join For Free

yesterday i got nerd-sniped into solving the fizzbuzz problem without using if sentences.

fizzbuzz, no ifs: [{0: "fizzbuzz"}.get(x%(3*5), {0: "fizz"}.get(x%3, {0: "buzz"}.get(x%5))) for x in range(3, 100, 3)+range(5, 105, 5)]

— swizec (@swizec) september 16, 2012

unfortunately, a more careful inspection of the problem specification reveals this solution was incorrect.

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”.

luckily, that’s a very simple fix:

# codegolf version (90 characters):
[{0:"fizzbuzz"}.get(x%(3*5),{0:"fizz"}.get(x%3,{0:"buzz"}.get(x%5,x)))for x in range(101)]
 
# slightly more readable:
[{0: "fizzbuzz"}.get(x%(3*5), 
                     {0: "fizz"}.get(x%3, 
                                     {0: "buzz"}.get(x%5, x))) 
 for x in range(101)]

there you have it, a solution to the fizzbuzz problem that relies on python’s interactive command line interpreter thingy to do the printing, shaving off 4 characters for print .

the “trick”, if it can be called that, is relying on the native get method to return the correct string or fall back to the default value, which checks for the next string in the if-then-else chain.

i wish never to see code like that in something i have to maintain. ever.

the challenge

your challenge, should you choose to accept it, is to produce a shorter fizzbuzz solution without using if sentences. i will buy a bottle of beer for the winner.

the best scala solution so far is 106 characters, by @hairyfotr:

1 to 100 map(a=>println((a%3,a%5)match{case(0,0)=>"fizzbuzz"case(0,_)=>"fizz"case(_,0)=>"buzz"case _=>a})) /cc

no really, i will send a bottle of beer anywhere in the world if you write a shorter solution. of course, if feasible, i would prefer to take you out for a beer than use snail mail :)

beer bottles by photos for fun
Strings Bottle (web framework) Data Types Printing Python (language) Mail (Apple) Scala (programming language) Command (computing)

Published at DZone with permission of Swizec Teller, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Utilize OpenAI API to Extract Information From PDF Files
  • Apache Kafka vs. Memphis.dev
  • RabbitMQ vs. Memphis.dev
  • PHP vs React

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: