private fun sumAndAvgOfDigits(n: String) {
val arrOfInt= n.map {"$it".toInt()}
print("${arrOfInt.sum()} ${arrOfInt.average()}")
}
@JvmStatic
fun main(args: Array<String>) {
sumAndAvgOfDigits("12345")
}
Another solution:
private fun sumAndAvgOfDigits(n: String) {
val arr = n.toList()
val sum = arr.sumBy { "$it".toInt() }
val avg = (sum.toDouble()/arr.size)
print("$sum $avg")
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…