Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
2
Help us understand the problem. What is going on with this article?

More than 3 years have passed since last update.

@shiro01

Python3のlambdaでfizzbuzzを1行にしてみた。

たまたまlambdaの書き方を復習したのでfizzbuzzしてみました。
仕事とかでこんなの書いたら怒られそう。

コード
print([(lambda x: 0 if x == 0 else "fizzbuzz" if x % 15 == 0 else "fizz" if x % 3 == 0 else "buzz" if x % 5 == 0 else x)(x) for x in range(35)])
結果
[0, 1, 2, 'fizz', 4, 'buzz', 'fizz', 7, 8, 'fizz', 'buzz', 11, 'fizz', 13, 14, 'fizzbuzz', 16, 17, 'fizz', 19, 'buzz', 'fizz', 22, 23, 'fizz', 'buzz', 26, 'fizz', 28, 29, 'fizzbuzz', 31, 32, 'fizz', 34]

長くなるとやっぱり読みにくい。

Pythonで1行fizzbuzzするなら、こちらの記事のfizzbuzzを参考にしたほうがよさそう。
https://qiita.com/gyu-don/items/befaee1729e84b5bf2c9

引用
for i in range(1,101):print("Fizz"*(i%3<1)+"Buzz"*(i%5<1)or str(i))
2
Help us understand the problem. What is going on with this article?
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
2
Help us understand the problem. What is going on with this article?