![]() |
|
[Tut] Python One Line FizzBuzz - Printable Version +- Sick Gaming (https://sickgaming.net) +-- Forum: Programming (https://sickgaming.net/forum-76.html) +--- Forum: Python (https://sickgaming.net/forum-83.html) +--- Thread: [Tut] Python One Line FizzBuzz (/thread-97156.html) |
[Tut] Python One Line FizzBuzz - xSicKxBot - 09-10-2020 Python One Line FizzBuzz The FizzBuzzproblem 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:
Example: The first 15 numbers of the FizzBuzz sequence are the following. Code: 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: https://repl.it/@finxter/RashMintyConstants?lite=true 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). Code: 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 Code: joinThe Code: string.join(iterable)Code: iterableCode: stringCode: '-'.join(['hello', 'world'])Code: 'hello-world'So, what’s the iterable, you pass into the Code: join()Code: expression for variable in contextCode: range()Code: expression for i in range(1, 101)It consists of three elements:
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): Code: 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?
Slicing is a concept to carve out a substring from a given string. Use slicing notation Code: s[start:stop:step]Code: stepCode: startCode: stopCode: start=0Code: stop=len(lst)Code: step=1Code: s[2:4]Code: 'hello'Code: 'll'Code: s[:3:2]Code: 'hl'In the example, you have the slicing operation Code: 'FizzBuzz'[i*i%3*4:8--i**4%5]
So, there are four cases:
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): Code: 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 Code: iPython One-Liners Book Python programmers will improve their computer science skills with these useful one-liners. ![]() Python One-Linerswill 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. Get your Python One-Liners Now!! |