What I thought about writing Python. Update when you think of it.
Reference) Detailed python comprehension
fizzbuzz=[]
for i in range(1,16):
if i%15==0:
fizzbuzz.append("fizzbuzz")
elif i%3==0:
fizzbuzz.append("fizz")
elif i%5==0:
fizzbuzz.append("buzz")
else:
fizzbuzz.append(i)
#>>> [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']
If this is intensional notation
["fizzbuzz" if i%15==0 else "fizz" if i%3==0 else "buzz" if i%5==0 else i for i in range(1,16)]
#>>> [1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz']
It's hard to read because it says the road to darkness, but maybe it's still readable if you set it below.
["fizzbuzz" if i % 15 == 0
else "fizz" if i % 3 == 0
else "buzz" if i % 5 == 0
else i
for i in range(1, 16)]
The for line comes with an iterator, so if you want to know the contents of i, look at the bottom. Above that is the value when the conditional expression is satisfied.
Recommended Posts