Fizz Buzz on Kotlin

スポンサーリンク

How do you think your code structure and logic?

My policy:
– Exclude magic number “15”.
– Consider dividing with a view codes and creating data codes.
– Use StringBuilder for a string combination.


(1..100).map {

  // P
  StringBuilder().apply {
    if (it % 3 == 0) append("Fizz")
    if (it % 5 == 0) append("Buzz")
    if (isEmpty()) append(it)
  }.toString()

}.forEach {

  // V
  println(it)

}


1
2
Fizz
4
Buzz
Fizz
7
8
Fizz
Buzz
11
Fizz
13
14
FizzBuzz
16
...

How do you assemble standard code fragments about this common simple challenge?

Fizz buzz – Wikipedia


関連ワード:  androidkotlin