• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
[Tut] Python One Line FizzBuzz

Python One Line FizzBuzz

The FizzBuzz problem is a common exercise posed in code interviews to test your proficiency in writing simple Python code.

Problem: Print all numbers from 1-100 to the shell with three exceptions:

• For each number divisible by three you print "Fizz",
• For each number divisible by five you print "Buzz", and
• For each number divisible by three and five you print "FizzBuzz".

Example: The first 15 numbers of the FizzBuzz sequence are the following.

1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
...

How to write a Python one-liner that solves this problem?

Here’s an interactive overview:

Exercise: Do both one-liners produce the same results? Run the code to check!

Let’s dive into those one-liners to gain a deeper understanding and improve your Python skills!

## FizzBuzz One-Liner 1: Generator Expression + String Concatenation + Short Circuiting

The following one-liner solves the problem in an elegant way using a fine understanding of more advanced Python features (source).

print('\n'.join('Fizz' * (i%3==0) + 'Buzz' * (i%5==0) or str(i) for i in range(1,101)))

The one-liner creates a string using the join function with the newline character as a delimiter. Here’s a short explanation of the function:

The string.join(iterable) method concatenates all the string elements in the iterable (such as a list, string, or tuple) and returns the result as a new string. The string on which you call it is the delimiter string—and it separates the individual elements. For example, '-'.join(['hello', 'world']) returns the joined string 'hello-world'.

So, what’s the iterable, you pass into the join() function? It’s a generator expression of the form: expression for variable in context. You go over all integer values in the context 1 to 100 using the range() function. So, you obtain the remaining expression for i in range(1, 101). What’s the expression part?

It consists of three elements:

• 'Fizz' * (i%3==0) — The modulo expression i%3==0 returns True only if the integer i is divisible by 3, otherwise it returns False. So, you multiply the string 'Fizz' either with True (=1) or with False (=0). As a result, you obtain the empty string '' in all cases except if the integer i is divisible by 3—in which case you obtain the string 'Fizz'.
• 'Buzz' * (i%5==0)— The modulo expression i%5==0 returns True only if the integer i is divisible by 5, otherwise it returns False. So, you multiply the string 'Buzz' either with True (=1) or with False (=0). As a result, you obtain the empty string '' in all cases except if the integer i is divisible by 5—in which case you obtain the string 'Buzz'.
• You use string concatenation to glue together the previously obtained strings. In most cases, this will be the empty string. If i is divisible by 3, you obtain the string 'Fizz'. If i is divisible by 5, you obtain the string 'Buzz'. And if i is divisible by 3 and 5, you obtain the string 'FizzBuzz'.
• or str(i) — In the case you obtained a non-empty string in {'Fizz', 'Buzz', 'FizzBuzz'} in the previous step, the or operation simply returns this string. This is called short circuiting—and it’s used in many programming languages such as Python to improve efficiency of logical operations.
• But if the string is empty, it is interpreted as a logical False. Thus, Python returns the second operand of the or operation. The second operand simply is the string representation of the integer i.

A very interesting implementation of the FizzBuzz problem indeed!

## FizzBuzz One-Liner 2: Slicing

An alternative is given in the following nice one-liner (source):

for i in range(1, 101): print('FizzBuzz'[i*i%3*4:8--i**4%5] or i)

Wow—what a short and concise one-liner solution! But how does it work?

• You iterate over all values from i=1 to i=100 and print a string. So far so good.
• You use the or operation and slicing to determine the string 'FizzBuzz'[start:end] or i generates the output.
• You use the property of short circuiting in Python: If 'FizzBuzz'[start:end] is empty, the integer i is returned, otherwise, the non-empty string is returned.
• You carve out a substring from 'FizzBuzz' using slicing as follows.

Slicing is a concept to carve out a substring from a given string. Use slicing notation s[start:stop:step] to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values (start=0, stop=len(lst), step=1). For example, the expression s[2:4] from string 'hello' carves out the slice 'll' and the expression s[:3:2] carves out the slice 'hl'.

In the example, you have the slicing operation 'FizzBuzz'[i*i%3*4:8--i**4%5].

• start = i*i%3*4 — Note that the multiplication * and modulo operation % have the same priority, so they are evaluated from left to right. If integer i is divisible by 3, i*i is also divisible by 3, and the start index is 0. In all other cases, the start index is 4. Thus, the slice either starts with 'Fizz' or 'Buzz'.
• stop = 8--i**4%5 — This is 4 in all cases except if the number i is divisible by 5, in which case this is 8.

So, there are four cases:

• The number is divisible only by 3: start=0, stop=4 –> 'Fizz'
• The number is divisible only by 5: start=4, stop=8 –> 'Buzz'
• The number is divisible by both 3 and 5: start=0, stop=8 –> 'FizzBuzz'
• The number is divisible by neither 3 nor 5: start = 4, stop=4 –> ''

Phew! This was a hard nut to crack, wasn’t it?

## Python One-Liner 3: Map + Lambda

You can find detailed tutorials on the map and lambda functions here:

Those two functions can be used to solve the FizzBuzz problem (source):

print(list(map(lambda i: "Fizz"*(i%3==0)+"Buzz"*(i%5==0) or str(i), range(1,101))))

It’s similar to Method 1 and by now you’re able to figure it out. Think of the different values the integer i can take.

## Python One-Liners Book

Python programmers will improve their computer science skills with these useful one-liners.

Python One-Liners will teach you how to read and write “one-liners”: concise statements of useful functionality packed into a single line of code. You’ll learn how to systematically unpack and understand any line of Python code, and write eloquent, powerfully compressed Python like an expert.

The book’s five chapters cover tips and tricks, regular expressions, machine learning, core data science topics, and useful algorithms. Detailed explanations of one-liners introduce key computer science concepts and boost your coding and analytical skills. You’ll learn about advanced Python features such as list comprehension, slicing, lambda functions, regular expressions, map and reduce functions, and slice assignments. You’ll also learn how to:

Leverage data structures to solve real-world problems, like using Boolean indexing to find cities with above-average pollution
Use NumPy basics such as array, shape, axis, type, broadcasting, advanced indexing, slicing, sorting, searching, aggregating, and statistics
Calculate basic statistics of multidimensional data arrays and the K-Means algorithms for unsupervised learning
Create more advanced regular expressions using grouping and named groups, negative lookaheads, escaped characters, whitespaces, character sets (and negative characters sets), and greedy/nongreedy operators
Understand a wide range of computer science topics, including anagrams, palindromes, supersets, permutations, factorials, prime numbers, Fibonacci numbers, obfuscation, searching, and algorithmic sorting

By the end of the book, you’ll know how to write Python at its most refined, and create concise, beautiful pieces of “Python art” in merely a single line.

https://www.sickgaming.net/blog/2020/09/...-fizzbuzz/

 Possibly Related Threads… Thread Author Replies Views Last Post [Tut] Overriding in Python xSicKxBot 0 4 Today, 04:49 AM Last Post: xSicKxBot [Tut] How to Install PIL/Pillow in Python? A Helpful Illustrated Guide xSicKxBot 0 5 10-27-2020, 12:00 AM Last Post: xSicKxBot [Tut] How to Read a File Line-By-Line and Store Into a List? xSicKxBot 0 6 10-24-2020, 06:12 PM Last Post: xSicKxBot [Tut] How To Kill A Thread In Python? xSicKxBot 0 8 10-24-2020, 05:40 AM Last Post: xSicKxBot [Tut] Python’s NameError: name ‘xxx’ is not defined — How to Fix This Stupid Bug? xSicKxBot 0 18 10-16-2020, 10:30 PM Last Post: xSicKxBot [Tut] Python IndentationError: unexpected indent (How to Fix This Stupid Bug) xSicKxBot 0 21 10-10-2020, 08:24 PM Last Post: xSicKxBot [Tut] Yield Keyword in Python – A Simple Illustrated Guide xSicKxBot 0 22 10-09-2020, 06:13 PM Last Post: xSicKxBot [Tut] Python Reverse List with Slicing — An Illustrated Guide xSicKxBot 0 41 10-02-2020, 04:24 AM Last Post: xSicKxBot [Tut] How to Remove Duplicates From a Python List While Preserving Order? xSicKxBot 0 38 10-01-2020, 02:21 AM Last Post: xSicKxBot [Tut] How To Update A Key In A Dictionary In Python If The Key Doesn’t Exist? xSicKxBot 0 45 09-29-2020, 09:02 PM Last Post: xSicKxBot

Forum Jump:

Become a Patron!
 Upcoming Events

 Latest Threads (Indie Deal) Tropico 6, Assassin's Creed... Last Post: xSicKxBot Today 04:49 PM » Replies: 0 » Views: 3 Xbox Wire - Xbox Insider Release Notes –... Last Post: xSicKxBot Today 04:44 PM » Replies: 0 » Views: 1 News - Sony delays PS5 launch title Dest... Last Post: xSicKxBot Today 04:44 PM » Replies: 0 » Views: 2 News - Another Halo Infinite Director Le... Last Post: xSicKxBot Today 11:35 AM » Replies: 0 » Views: 4 News - An Exciting New Trailer For No Mo... Last Post: xSicKxBot Today 05:25 AM » Replies: 0 » Views: 3 News - Tropico 6 Lands On Switch In The ... Last Post: xSicKxBot Today 05:25 AM » Replies: 0 » Views: 4 News - Costco's Black Friday 2020 Ad Jus... Last Post: xSicKxBot Today 05:25 AM » Replies: 0 » Views: 4 [Tut] Overriding in Python Last Post: xSicKxBot Today 04:49 AM » Replies: 0 » Views: 4 (Indie Deal) ?FREE Dyna Bomb, Big Quiz E... Last Post: xSicKxBot Today 04:49 AM » Replies: 0 » Views: 5 ODIN 2 Synthesizer VST Last Post: xSicKxBot Yesterday 11:24 PM » Replies: 0 » Views: 5