4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

if文を使わずにFizzBuzzしてみた

Last updated at Posted at 2023-02-11

次のお二人の記事

を読んで、if文を使わずにFizzBuzzを解こうとして、剰余演算子%も使わずに、次のようなコードを書いたことを思い出しました。

def fizzbuzz(n):
    def set_fizzbuzz(d, s):
        for i in range(0, n, d):
            fb[i] = s

    n += 1
    fb = [i for i in range(n)]
    set_fizzbuzz(3, "Fizz")
    set_fizzbuzz(5, "Buzz")
    set_fizzbuzz(15, "FizzBuzz")
    return fb[1:]


print(*fizzbuzz(100), sep="\n")

 今、計算量なんか気にせずにifを使わないことだけを考えて書くのが“マイブーム”になっているんです。

追記(2023/7/7)

その後、こんなのもできました。

for i in range(100):
    print("Fizz"*(i%3==2) + "Buzz"*(i%5==4) or i+1)
4
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?