class Solution {
fun solution(num_list: IntArray): Int {
var totalMultiply = 1
val totalSquare = num_list.sumOf{it} * num_list.sumOf{it}
val resultMutuply = num_list.forEachIndexed{ idx, _ ->
totalMultiply = totalMultiply * num_list[idx]
}
return if(totalMultiply < totalSquare) 1 else 0
}
}
class Solution {
fun solution(num_list: IntArray): Int = if (num_list.multiple() > num_list.sum().pow()) 0 else 1
fun IntArray.multiple(): Int {
var result = 1
this.forEach {
result *= it
}
return result
}
fun Int.pow(): Int = this * this
}
sum()
val numbers = listOf(1, 2, 3, 4)
println(numbers.sum()) // 출력: 10
sumOf
data class Item(val price: Double, val quantity: Int)
val items = listOf(Item(10.0, 2), Item(15.5, 3))
println(items.sumOf { it.price * it.quantity }) // 출력: 71.5
forEachIndexed
val names = listOf("Alice", "Bob", "Charlie")
names.forEachIndexed { index, name ->
println("Index: $index, Name: $name")
}
// 출력:
// Index: 0, Name: Alice
// Index: 1, Name: Bob
// Index: 2, Name: Charlie
*= 곱셈 할당 연산자
var value = 5
value *= 3 // value = value * 3
println(value) // 출력: 15