Friday, December 21, 2018

FizzBuzz in Python with nested conditional expressions and a generator expression

- By Vasudev Ram - Online Python training / SQL training / Linux training

The FizzBuzz problem [1] is known to those who test/interview programming candidates, as one simple way to filter out unsuitable people early. One keeps on coming across mentions of it on tech sites.

Here is a way of doing FizzBuzz in Python, not the most obvious way (which would be to use a for loop with an if/elif/else statement in it - although, of course, other ways are possible too). Instead, this way uses Python's language feature of conditional expressions:
# Fizzbuzz version using nested Python conditional expressions:
# Author: Vasudev Ram
# Web site:
# Blog:
# Twitter:

for i in range(1, 101):
    print "FizzBuzz" if i % 15 == 0 else "Buzz" if i % 5 == 0 \
    else "Fizz" if i % 3 == 0 else str(i),
Output (formatted a bit to fit on the screen):
$ python
1 2 Fizz 4 Buzz Fizz 7 8 Fizz Buzz 11 Fizz 13 14 FizzBuzz \
16 17 Fizz 19 Buzz Fizz 22 23 Fizz Buzz 26 Fizz 28 29 FizzBuzz \
31 32 Fizz 34 Buzz Fizz 37 38 Fizz Buzz 41 Fizz 43 44 FizzBuzz \
46 47 Fizz 49 Buzz Fizz 52 53 Fizz Buzz 56 Fizz 58 59 FizzBuzz \
61 62 Fizz 64 Buzz Fizz 67 68 Fizz Buzz 71 Fizz 73 74 FizzBuzz \
76 77 Fizz 79 Buzz Fizz 82 83 Fizz Buzz 86 Fizz 88 89 FizzBuzz \
91 92 Fizz 94 Buzz Fizz 97 98 Fizz Buzz

Here's another use of such conditional expressions:

Using nested conditional expressions to classify characters

Here is another Fizzbuzz version that uses both nested Python conditional expressions and a generator expression (again, formatted):
# FizzBuzz with a Python nested conditional expression and 
# a generator expression.
# Author: Vasudev Ram
# Web site:
# Blog:
# Twitter:

print(" ".join("FizzBuzz" if i % 15 == 0 else "Buzz" if i % 5 == 0 else "Fizz" if i % 3 == 0 \
else str(i) for i in range(1, 101)))
It gives the same output as the earlier version above.

What's the subtle aspect of the FizzBuzz problem that can trip up a naive candidate?

The post by Jeff Atwood about FizzBuzz (below) is interesting.

- Enjoy.

[1] References:

Fizz Buzz (Wikipedia)

Jeff Atwood (2007-02-26): Why Can't Programmers.. Program?

Imran Ghory (2007-01-24): "Using FizzBuzz to Find Developers who Grok Coding."

- Vasudev Ram - Online Python training and consulting

I conduct online courses on Python programming, Unix / Linux commands and shell scripting and SQL programming and database design, with course material and personal coaching sessions.

The course details and testimonials are here.

Contact me for details of course content, terms and schedule.

Try FreshBooks: Create and send professional looking invoices in less than 30 seconds.

Getting a new web site or blog, and want to help preserve the environment at the same time? Check out web hosting.

Sell your digital products via DPD: Digital Publishing for Ebooks and Downloads.

Learning Linux? Hit the ground running with my vi quickstart tutorial. I wrote it at the request of two Windows system administrator friends who were given additional charge of some Unix systems. They later told me that it helped them to quickly start using vi to edit text files on Unix. Of course, vi/vim is one of the most ubiquitous text editors around, and works on most other common operating systems and on some uncommon ones too, so the knowledge of how to use it will carry over to those systems too.

Check out WP Engine, powerful WordPress hosting.

Sell More Digital Products With SendOwl.

Get a fast web site with A2 Hosting.

Creating online products for sale? Check out ConvertKit, email marketing for online creators.

Teachable: feature-packed course creation platform, with unlimited video, courses and students.

Managed WordPress hosting with Flywheel.

Posts about: Python * DLang * xtopdf

My ActiveState Code recipes

Follow me on:

No comments: