Yes..! let's do Fizz Buzz
using python
But first. What is Fizz Buzz
?
The "Fizz-Buzz test" is an interview question designed to help filter out the 99.5% of programming job candidates who can't seem to program their way out of a wet paper bag.
Fizz Buzz Test
Si, vamos a programar
Fizz Buzz
enpython
Pero primero. Que esFizz Buzz
?
Fizz Buzz test is una pregunta diseรฑada para una entrevista en lenguage de programacion.
How this is done?
Check for reminder between two numbers. >>> 5 % 3 = 2
- Let's create a list of numbers.
nums = [x for x in range(1, 50)]
We need to iterate the list and check the reminder of each number, if reminder of 3 and 5 print 'Fizz Buzz'
if reminder of 3 print 'Fizz'
or reminder of 5 print 'Buzz'
else print the number
Let's create a function that takes the list nums
and do the job...
def fizzBuzz(nums):
for i in nums:
if i % 3 == 0 and i % 5 == 0:
print('Fizz Buzz')
elif i % 3 == 0:
print('Fizz')
elif i % 5 == 0:
print('Buzz')
else:
print(i)
Great! How is the output look like?
use and ide, this output is too long for this post..!!!
Let's do some refactor to this code. We are going to use shorter couple ways to do this block of code.
Fantastico! Como se ve el resultado de este codigo?
use un ide, este resultado es muy largo
Vamos a refactorizar este codio un poco. Usaremos lineas mas cortas.
def fizz_buzz(nums):
for i in nums:
fizz_buzz = 'Fizz Buzz' if i % 3 == 0 and i % 5 == 0 else ''
fizz = 'Fizz' if i % 3 == 0 else ''
buzz = 'Buzz' if i % 5 == 0 else ''
print(f'{fizz_buzz}' or f'{fizz}' or f'{buzz}' or i)
We can be even shorter than this last block of code.. Let see
fizz_buzz = ['Fizz' * (i % 3 == 0) + 'Buzz' * (i % 5 == 0) or i for i in nums]
print(fizz_buzz)
Yup! One line and is done...
This might not be the better way to approach Fizz Buzz
if you can contribute with a different or
better way, please do.
Cheers..๐บ
Top comments (0)