開発環境
- macOS Mojave - Apple (OS)
- Windows 10 Pro (OS)
- IntelliJ IDEA CE(Community Edition) (IDE(統合開発環境))
- Kotlin (プログラミング言語)
Head First Kotlin: A Brain-Friendly Guide (Dawn Griffiths(著)、David Griffiths(著)、O'Reilly Media)のChapter 12(built-in higher-order functions - Power Up Your Code)、Mixed Messages(391)の解答を求めてみる。
コード
fun main() { val myMap = mapOf("A" to 4, "B" to 3, "C" to 2, "D" to 1, "E" to 2) var x1 = "" var x2 = 0 println("1.") x1 = myMap.keys.fold("") { x, y -> x + y } x2 = myMap.entries.fold(0) { x, y -> x * y.value } println("$x1$x2" == "ABCDE0") x1 = "ABCDE" x2 = 0 println("2.") x2 = myMap.values.groupBy { it }.keys.sumBy { it } println("$x1$x2" == "ABCDE10") x1 = "ABCDE" println("3.") x2 = myMap.values.fold(12) { x, y -> x - y } println("$x1$x2" == "ABCDE0") x1 = "ABCDE" x2 = 0 println("4.") x2 = myMap.entries.fold(1) { x, y -> x * y.value } println("$x1$x2" == "ABCDE48") x1 = "ABCDE" x2 = 0 println("4.") x1 = myMap.values.fold("") { x, y -> x + y } println("$x1$x2" == "432120") x1 = "ABCDE" x2 = 0 println("5.") x1 = myMap.values .fold(0) { x, y -> x + y } .toString() x2 = myMap.keys.groupBy { it }.size println("$x1$x2" == "125") }
入出力結果
1. true 2. true 3. true 4. true 4. true 5. true Process finished with exit code 0
0 コメント:
コメントを投稿