fizzbuzz list comprehension

  • Last Update :
  • Techknowledgy :

In python you can replicate a string by using the multiplication operator:

print('aaa' * 3) # aaaaaaaaa

Also, boolean values are implicitly casted to integers on multiplication. Thus, if you do

"Fizz" * (not i % 3)

List comprehension is expressed as

  L = [mapping - expression
     for element in source - list
     if filter - expression
  ]

now, replace "for element in source-list" part with

 for i in range(1, 100)

"mapping-expression" here is

 "Fizz" * (not i % 3) + "Buzz" * (not i % 5) or i

when the booleans returned from (not i % 3) or (not i % 5) is multiplied with strings "Fizz" and "Buzz":

  "Fizz" * True # returns "Fizz"
  "Buzz" * False # returns ""

then the Strings returned above are concatenated

   "Fizz" + ""

You can use:

["Fizzbuzz"
   if (i % 3 == 0 and i % 5 == 0)
   else "Fizz"
   if (i % 3 == 0)
   else "Buzz"
   if (i % 5 == 0)
   else i
   for i in x
]
>>> 3 % 3
0
   >>>
   not 3 % 3
True
   >>>
   >>>
   3 % 2
1
   >>>
   not 3 % 2
False
   >>>
   >>>
   if 0:
   ...print 'true'
   ...
   >>>
   if 1:
   ...print 'true'
   ...
   true >>>
   False * 'Fizz'
'' >>>
True * 'Fizz'
'Fizz'

My personal favorite:

#!python

from __future__
import print_function
def fizz_buzz(lower = 1, upper = 101):
   return ["Fizz" * (x % 3 == 0) + "Buzz" * (x % 5 == 0) or str(x)\
      for x in range(lower, upper)
   ]

print('\n'.join(fizz_buzz()))

I think the following approach helps me understanding the list comprehension structure.

def fizzBuzz(upper_limit):

   def mapper(a):
   result = ''
if a % 3 == 0:
   result = result + 'Fizz'
if a % 5 == 0:
   result = result + 'Buzz'
return result

return [mapper(x) if mapper(x)
   else x
   for x in range(1, upper_limit + 1)
]

fizzBuzz(16)[1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz', 16]

Suggestion : 2

June 18, 2021

So, we saw how we can solve fizzbuzz, but if you are challenged to solve it in one line then don’t worry we got you covered. I will show you three ways to solve it. The last two solutions are picked from Github.
Also read –> How to make your own audiobook from pdf using python

# fizzbuzz using list comprehension(easy)
fizzbuzz = ['fizzbuzz'
   if v % 3 == 0 and v % 5 == 0
   else 'fizz'
   if v % 3 == 0
   else 'buzz'
   if v % 5 == 0
   else v
   for v in range(1, 51)
]
print('First way\n', fizzbuzz)

# fizzbuzz using list comprehension(intermediate) from github
fizzbuzz = ["Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or str(i) for i in range(1, 51)]
print('\n Second way\n', fizzbuzz)

# fizzbuzz using lambda - (intermediate) from github
fizzbuzz = list(map(lambda i: "Fizz" * (i % 3 == 0) + "Buzz" * (i % 5 == 0) or str(i), range(1, 51)))
print('\n Third way\n', fizzbuzz)

Suggestion : 3

In the comprehension, it loops for every anycodings_fizzbuzz number from 1 to 99, and if i%3 anycodings_fizzbuzz evaluates to True or i=3; 3%3, then anycodings_fizzbuzz "Fizz" is printed and the same is true anycodings_fizzbuzz for "Buzz" if i%5 == 0.,I think the following approach helps me anycodings_fizzbuzz understanding the list comprehension anycodings_fizzbuzz structure.,The same goes for Buzz, and the result anycodings_fizzbuzz for each i in the range is just the anycodings_fizzbuzz concatenation of the two.,when i is divisible by 3 or 5, i % 3 and anycodings_fizzbuzz i % 5 returns 0, any other integer anycodings_fizzbuzz retuned when i is not divisible.

I was messing around with some different anycodings_python fizz buzz scripts as I learn python. I came anycodings_python across this one which works great but I anycodings_python can't decipher how it works. I know how the anycodings_python normal fizz buzz works with a for loop and anycodings_python "if i % 3 == 0 and i % 5 == 0". What has me anycodings_python stumped is how "Fizz"(not i%3) + "Buzz"(not anycodings_python i%5)" works.

x = ["Fizz" * (not i % 3) + "Buzz" * (not i % 5) or i
   for i in range(1, 100)
]

In python you can replicate a string by anycodings_fizzbuzz using the multiplication operator:

print('aaa' * 3) # aaaaaaaaa

Also, boolean values are implicitly anycodings_fizzbuzz casted to integers on multiplication. anycodings_fizzbuzz Thus, if you do

"Fizz" * (not i % 3)

List comprehension is expressed as

  L = [mapping - expression
     for element in source - list
     if filter - expression
  ]

now, replace "for element in anycodings_fizzbuzz source-list" part with

 for i in range(1, 100)

"mapping-expression" here is

 "Fizz" * (not i % 3) + "Buzz" * (not i % 5) or i

when the booleans returned from (not i % anycodings_fizzbuzz 3) or (not i % 5) is multiplied with anycodings_fizzbuzz strings "Fizz" and "Buzz":

  "Fizz" * True # returns "Fizz"
  "Buzz" * False # returns ""

then the Strings returned above are anycodings_fizzbuzz concatenated

   "Fizz" + ""

You can use:

["Fizzbuzz"
   if (i % 3 == 0 and i % 5 == 0)
   else "Fizz"
   if (i % 3 == 0)
   else "Buzz"
   if (i % 5 == 0)
   else i
   for i in x
]
>>> 3 % 3
0
   >>>
   not 3 % 3
True
   >>>
   >>>
   3 % 2
1
   >>>
   not 3 % 2
False
   >>>
   >>>
   if 0:
   ...print 'true'
   ...
   >>>
   if 1:
   ...print 'true'
   ...
   true >>>
   False * 'Fizz'
'' >>>
True * 'Fizz'
'Fizz'

My personal favorite:

#!python

from __future__
import print_function
def fizz_buzz(lower = 1, upper = 101):
   return ["Fizz" * (x % 3 == 0) + "Buzz" * (x % 5 == 0) or str(x)\
      for x in range(lower, upper)
   ]

print('\n'.join(fizz_buzz()))